- Create a string called str to input your sentence. As usual, the last character will be a null character:
char str[255];
- Enter a sentence of your choice:
printf("Enter a sentence: ");
- Execute the gets function to accept the sentence with blank spaces between the words, and initialize the i variable to 0, since each character of the sentence will be accessed through i:
gets(str);
i=0
- Execute a while loop to access each letter of the sentence one by one, until the null character in the sentence is reached:
while(str[i]!='\0')
{
{ …
}
}
i++;
- Check each letter to verify whether it is a lowercase vowel. If the accessed character is a lowercase vowel, then the value 32 is subtracted from the ASCII value of that vowel to convert it to uppercase:
if(str[i]=='a' ||str[i]=='e' ||str[i]=='i' ||str[i]=='o' ||str[i]=='u')
str[i]=str[i]-32;
- When all of the letters of the sentence have been accessed, then simply display the entire sentence.
The convertvowels.c program for converting the lowercase vowels in a sentence to uppercase is as follows:
#include <stdio.h>
void main()
{
char str[255];
int i;
printf("Enter a sentence: ");
gets(str);
i=0;
while(str[i]!='\0')
{
if(str[i]=='a' ||str[i]=='e' ||str[i]=='i' ||str[i]=='o'
||str[i]=='u')
str [i] = str [i] -32;
i++;
}
printf("The sentence after converting vowels into uppercase
is:\n");
puts(str);
}
Now, let's go behind the scenes to understand the code better.