Menu
Coddy logo textTech

Convert to Uppercase

Part of the Logic & Flow section of Coddy's C journey — lesson 23 of 63.

challenge icon

Challenge

Easy

Extend your text utility program from the previous lesson to add uppercase conversion functionality. Your program should:

  1. Prompt the user to enter a short sentence by printing: Enter a sentence: (no newline at the end)
  2. Declare a character array named sentence with 200 elements to store the input
  3. Use scanf with the %s format specifier to read a single word from the user
  4. Print the stored word in the format: You entered: [word]
  5. Use strlen() to calculate the total number of characters in the word
  6. Print the character count in the format: Character count: [count]
  7. Print the length using strlen() in the format: Length: [length]
  8. Iterate through each character in the word using a loop to count vowels
  9. For each character, check if it is a vowel (a, e, i, o, u) in both lowercase and uppercase
  10. Keep a running count of vowels found
  11. Print the vowel count in the format: Vowel count: [count]
  12. Print Uppercase: and then use a loop to convert and print each character in the word to uppercase using the toupper() function

Remember to include the <string.h> header to use the strlen() function and the <ctype.h> header to use the toupper() function.

About toupper():
The toupper() function from <ctype.h> converts a single character to its uppercase equivalent. If the character is already uppercase or is not a letter, it is returned unchanged.
Example: toupper('a') returns 'A', and toupper('Z') returns 'Z'.
You can use it inside a loop to convert each character one at a time: printf("%c", toupper(sentence[i]));

Important: The prompt Enter a sentence: should not be followed by a newline. Use printf("Enter a sentence: "); without \n.

Your output should display the results in the following format:

Enter a sentence: You entered: [word]
Character count: [count]
Length: [length]
Vowel count: [count]
Uppercase: [WORD]

For example, if the input is:

Programming

Your output should be:

Enter a sentence: You entered: Programming
Character count: 11
Length: 11
Vowel count: 3
Uppercase: PROGRAMMING

This challenge builds upon the previous lesson by adding uppercase conversion functionality using the toupper() function from the <ctype.h> library. You will need to use a loop to iterate through each character in the string and convert it to uppercase, then print the transformed result.

Try it yourself

#include <stdio.h>
#include <string.h>

int main() {
    printf("Enter a sentence: \n");
    
    char sentence[200];
    scanf("%s", sentence);
    
    printf("You entered: %s\n", sentence);
    
    int char_count = strlen(sentence);
    printf("Character count: %d\n", char_count);
    
    int length = strlen(sentence);
    printf("Length: %d\n", length);
    
    int vowel_count = 0;
    for (int i = 0; i < strlen(sentence); i++) {
        char c = sentence[i];
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
            c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
            vowel_count++;
        }
    }
    
    printf("Vowel count: %d\n", vowel_count);
    
    return 0;
}

All lessons in Logic & Flow