Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Character Functions

The library ctype.h has functions that work with one character. Two of them change it:

  • toupper(c) gives the uppercase letter: toupper('a') is 'A';
  • tolower(c) gives the lowercase letter: tolower('Q') is 'q'.

Other characters stay as they are: toupper('5') is '5', and toupper('A') is 'A'.

#include <ctype.h>
#include <stdio.h>

int main(void) {
    char name[] = "eva";
    name[0] = toupper(name[0]);
    printf("%s\n", name);
    return 0;
}

This prints:

Eva

toupper doesn't change the character you give it. It gives you a new character, and you have to store it: name[0] = toupper(name[0]);. A line with only toupper(name[0]); compiles, but the name stays eva, because the new character is thrown away. (The result is a number of type int, the code of the character. When you store it in a char or print it with %c, it's a character again.)

Other functions answer a question about a character:

  • isupper(c): is it an uppercase letter?
  • isalpha(c): is it a letter?
  • isdigit(c): is it a digit, from '0' to '9'?

The answer is a number: 0 means no, and any other number means yes. So use them directly as a condition, as you know from the lesson on conditions: if (isdigit(c)). This loop counts the digits in a code:

char code[] = "R2D2";
int digits = 0;
for (int i = 0; code[i] != '\0'; i++) {
    if (isdigit(code[i])) {
        digits++;
    }
}
printf("Digits: %d\n", digits);

This prints:

Digits: 2

These functions only know the letters of the English alphabet, without accents. One more reason to keep the strings you go through character by character without accents.

Instructions

  1. 1

    Someone typed the city with Caps Lock on. Add #include <ctype.h> and fix city: change its first character to uppercase with toupper, and all the others to lowercase with tolower, in a loop that starts at the index 1. Then print it: Fixed: Prague.

  2. 2

    Below that, create char code[] = "B4X-27Q9";. Go through it in a loop, count its digits with isdigit and its letters with isalpha, and print Digits: 4, letters: 3.

Back

8/10

  1. 1. Text in a Variable
  2. 2. Characters of a String
  3. 3. Length of a String
  4. 4. Going Through a String
  5. 5. Room in the Array
  6. 6. Joining Strings
  7. 7. Comparing Strings
  8. 8. Character Functions
  9. 9. Aligned Columns
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.