A program that asks the user to enter a sentence with a maximum of 100 characters, and…
- User
A program that asks the user to enter a sentence with a maximum of 100 characters, and prints the number of letters, special marks, numbers, and spaces use c program
- Assistant
Here is a C program that fulfills your requirements:
#include <stdio.h> int main() { char sentence[101]; int letters = 0, special_chars = 0, numbers = 0, spaces = 0; printf("Enter a sentence (max 100 characters): "); fgets(sentence, sizeof(sentence), stdin); for (int i = 0; sentence[i] != '\0'; i++) { if ((sentence[i] >= 'a' && sentence[i] <= 'z') || (sentence[i] >= 'A' && sentence[i] <= 'Z')) { letters++; } else if (sentence[i] >= '0' && sentence[i] <= '9') { numbers++; } else if (sentence[i] == ' ') { spaces++; } else { special_chars++; } } printf("Number of letters: %d\n", letters); printf("Number of special marks: %d\n", special_chars); printf("Number of numbers: %d\n", numbers); printf("Number of spaces: %d\n", spaces); return 0; } - User
A program that asks the user to enter a sentence with a maximum of 100 characters, and prints the number of letters, special marks, numbers, and spaces c program use int char
- Assistant
Certainly! Here's an updated version of the previous program that uses
intdata type instead ofcharfor counting characters:#include <stdio.h> int main() { char sentence[101]; int letters = 0, special_marks = 0, numbers = 0, spaces = 0; printf("Enter a sentence (max 100 characters): "); fgets(sentence, sizeof(sentence), stdin); for (int i = 0; sentence[i] != '\0'; i++) { if ((sentence[i] >= 'a' && sentence[i] <= 'z') || (sentence[i] >= 'A' && sentence[i] <= 'Z')) { letters++; } else if (sentence[i] >= '0' && sentence[i] <= '9') { numbers++; } else if (sentence[i] == ' ') { spaces++; } else { special_marks++; } } printf("Number of letters: %d\n", letters); printf("Number of special marks: %d\n", special_marks); printf("Number of numbers: %d\n", numbers); printf("Number of spaces: %d\n", spaces); return 0; } - User
A program that asks the user to enter a sentence with a maximum of 50 characters, and prints the number of the most frequent characters in the sentence c program use int char
- Assistant
Certainly! Below is a C program that asks the user to enter a sentence with a maximum of 50 characters and prints the number of occurrences for the most frequent characters in the sentence:
#include <stdio.h> #include <string.h> #define MAX_LEN 51 int main() { char sentence[MAX_LEN]; int count[256] = {0}; int max_count = 0; printf("Enter a sentence (max 50 characters): "); fgets(sentence, sizeof(sentence), stdin); // Remove trailing newline character sentence[strcspn(sentence, "\n")] = '\0'; for (int i = 0; sentence[i] != '\0'; i++) { count[sentence[i]]++; if (count[sentence[i]] > max_count) { max_count = count[sentence[i]]; } } printf("Most frequent character(s) and their count:\n"); for (int i = 0; i < 256; i++) { if (count[i] > 0 && count[i] == max_count) { printf("%c: %d\n", i, count[i]); } } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.