Convert a String to Uppercase in C


Here is the program to convert a string to uppercase in C language,

Example

#include <stdio.h>
#include <string.h>
int main() {
   char s[100];
   int i;
   printf("
Enter a string : ");    gets(s);    for (i = 0; s[i]!='\0'; i++) {       if(s[i] >= 'a' && s[i] <= 'z') {          s[i] = s[i] -32;       }    }    printf("
String in Upper Case = %s", s);    return 0; }

Output

Enter a string : hello world!
String in Upper Case = HELLO WORLD!

In the program, the actual code of conversion of string to upper case is present in main() function. An array of char type s[100] is declared which will store the entered string by user.

Then, for loop is used to convert the string into upper case string and if block is used to check that if characters are in lower case then, convert them in upper case by subtracting 32 from their ASCII value.

for (i = 0; s[i]!='\0'; i++) {
   if(s[i] >= 'a' && s[i] <= 'z') {
      s[i] = s[i] -32;
   }
}

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 04-Oct-2023

25K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements