C Program to replace lowercase vowels to uppercase in a string

Here is the C program to replace lowercase vowels to uppercase in a string
#include<stdio.h>
#include<conio.h>
void main()
{         char str[200];
           int i=0;
           clrscr();
           printf(“Enter a String:”);
           gets(str);                      // can also use fgets(str,200,stdin);
           printf(“Actual String:”);
           puts(str);
           for(i=0;str[i]!=’\0′;i++)
           {         if(str[i]==’a’ ||str[i]==’e’ ||str[i]==’i’ ||str[i]==’o’ ||str[i]==’u’)
                      {          str[i]=str[i]-32;            //to convert lower case vowels to upper case
                       }
            }
            printf(“Converted string:%s”,str);
            getch();
}
Output
Logic of program
  • The vowels(a,e,i,o,u) in lowercase can be converted into uppercase by using the logic of ASCII value.
  • The ASCII value of lower characters(a-z) is 97-122 respectively and uppercase character(A-Z) is 65-90 respectively.
  • So here it is clearly seen the difference in ASCII values of lowercase and uppercase characters is 32.
  • So if the 32 is subtracted from the ASCII value of lowercase charater than it can be converted into uppercase character and vice-versa.
  • The loop used i.e for loop which will go to every single character of the string starting from i=0 to the null character(‘\0’).
  • If is used for making a condition that if the character in a string is vowel than it will follow the condition written in it and will convert it to uppercase.

You may also like...

Leave a Reply