You are here

C program to change case of a string

Strlwr function converts a string to lower case, and strupr function converts a string to upper case. Here we will change string case with and without strlwr, strupr functions. These functions convert case of alphabets and ignore other characters which may be present in a string.

Function strlwr in C

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6.    char string[1000];
  7.    
  8.    printf("Input a string to convert to lower case\n");
  9.    gets(string);
  10.    
  11.    printf("The string in lower case: %s\n", strlwr(string));
  12.    
  13.    return  0;
  14. }

Function strupr in C

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6.    char string[1000];
  7.    
  8.    printf("Input a string to convert to upper case\n");
  9.    gets(string);
  10.    
  11.    printf("The string in upper case: %s\n", strupr(string));
  12.    
  13.    return  0;
  14. }

Change string to upper case without strupr

  1. #include <stdio.h>
  2.  
  3. void upper_string(char []);
  4.  
  5. int main()
  6. {
  7.    char string[100];
  8.    
  9.    printf("Enter a string to convert it into upper case\n");
  10.    gets(string);
  11.    
  12.    upper_string(string);
  13.    
  14.    printf("The string in upper case: %s\n", string);
  15.      
  16.    return 0;
  17. }
  18.  
  19. void upper_string(char s[]) {
  20.    int c = 0;
  21.    
  22.    while (s[c] != '\0') {
  23.       if (s[c] >= 'a' && s[c] <= 'z') {
  24.          s[c] = s[c] - 32;
  25.       }
  26.       c++;
  27.    }
  28. }

Change string to lower case without strlwr

  1. #include <stdio.h>
  2.  
  3. void lower_string(char []);
  4.  
  5. int main()
  6. {
  7.    char string[100];
  8.    
  9.    printf("Enter a string to convert it into lower case\n");
  10.    gets(string);
  11.    
  12.    lower_string(string);
  13.    
  14.    printf("The string in lower case: %s\n", string);
  15.      
  16.    return 0;
  17. }
  18.  
  19. void lower_string(char s[]) {
  20.    int c = 0;
  21.    
  22.    while (s[c] != '\0') {
  23.       if (s[c] >= 'A' && s[c] <= 'Z') {
  24.          s[c] = s[c] + 32;
  25.       }
  26.       c++;
  27.    }
  28. }

You can also implement functions using pointers.

C program to change case from upper to lower and lower to upper

Below program changes case of alphabets if a lower case alphabet is found it is converted to upper and if an upper case is found it is converted to lower case.

  1. #include <stdio.h>
  2.  
  3. int main ()
  4. {
  5.    int c = 0;
  6.    char ch, s[1000];
  7.  
  8.    printf("Input a string\n");
  9.    gets(s);
  10.    
  11.    while (s[c] != '\0') {
  12.       ch = s[c];
  13.       if (ch >= 'A' && ch <= 'Z')
  14.          s[c] = s[c] + 32;
  15.       else if (ch >= 'a' && ch <= 'z')
  16.          s[c] = s[c] - 32;  
  17.       c++;  
  18.    }
  19.    
  20.    printf("%s\n", s);
  21.  
  22.    return 0;
  23. }

An output of the program:

  1. Input a string
  2. abcdefghijklmnopqrstuvwxyz{0123456789}ABCDEFGHIJKLMNOPQRSTUVWXYZ
  3. ABCDEFGHIJKLMNOPQRSTUVWXYZ{0123456789}abcdefghijklmnopqrstuvwxyz

If a digit or special character is present in a string, it is left as it is.