A program that asks the user to enter a sentence with a maximum of 50 characters, and…
- User
A program that asks the user to enter a sentence with a maximum of 50 characters, and prints the capital number c program
- Assistant
Here's a C program that asks the user to enter a sentence with a maximum of 50 characters and prints the number of capital letters in the sentence:
#include <stdio.h> #include <ctype.h> int main() { char sentence[51]; int capitalCount = 0; printf("Enter a sentence with a maximum of 50 characters: "); fgets(sentence, sizeof(sentence), stdin); for (int i = 0; sentence[i] != '\0'; i++) { if (isupper(sentence[i])) { capitalCount++; } } printf("The number of capital letters in the sentence is: %d\n", capitalCount); return 0; }This program uses the
fgets()function to read the sentence entered by the user. It then iterates through each character in the sentence using aforloop, checking if each character is an uppercase letter using theisupper()function from thectype.hlibrary. If a character is uppercase, thecapitalCountvariable is incremented.Finally, the program prints the number of capital letters found in the sentence.
- User
A program that asks the user to enter a sentence with a maximum of 50 characters, and prints the capital number c program use int and char just
- Assistant
Here's a simplified version of the C program that asks the user to enter a sentence with a maximum of 50 characters and prints the number of capital letters:
#include <stdio.h> int main() { char sentence[51]; int capitalCount = 0; printf("Enter a sentence with a maximum of 50 characters: "); fgets(sentence, sizeof(sentence), stdin); for (int i = 0; i < sizeof(sentence) && sentence[i] != '\0'; i++) { if (sentence[i] >= 'A' && sentence[i] <= 'Z') { capitalCount++; } } printf("The number of capital letters in the sentence is: %d\n", capitalCount); return 0; }In this version, I've used a character array
sentencewith a size of 51 to store the user's input. Thefgets()function is used to read the sentence from the user.The
forloop iterates through each character in thesentencearray. The loop conditioni < sizeof(sentence) && sentence[i] != '\0'ensures that we don't go beyond the size of the array and that we stop parsing the sentence when we encounter the null character ('\0') indicating the end of the string.Inside the loop, we check if each character is a capital letter by comparing it with the ASCII values of 'A' and 'Z'. If a character falls within this range, the
capitalCountvariable is incremented.Finally, the program prints the number of capital letters found in the sentence using
printf().
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.