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 number of small letters 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 small letters present in that sentence:
#include <stdio.h> int main() { char sentence[51]; // Assuming the maximum sentence length is 50 characters printf("Enter a sentence (max 50 characters): "); fgets(sentence, sizeof(sentence), stdin); int count = 0; for (int i = 0; sentence[i] != '\0'; i++) { if (sentence[i] >= 'a' && sentence[i] <= 'z') { count++; } } printf("Number of small letters: %d\n", count); return 0; }In this program, we first define a character array
sentenceof size 51 to store the user's input. We usefgetsto read the user input from the standard input (keyboard) and store it in thesentencearray.Then, we initialize a variable
countto keep track of the number of small letters in the sentence. We iterate through each character of the sentence using aforloop and check if the character is a small letter (ASCII value between 'a' and 'z'). If it is, we increment thecountvariable.Finally, we print the number of small letters using
printfstatement.Note: In this program, we assume that the user will input a valid sentence with a maximum length of 50 characters. If the user enters a longer sentence, it will be truncated to fit within the
sentencearray.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.