Input sentence and convert capital letters to small and vice versa in C
String Methods - Question 7
In this question, we will see how to input a sentence and convert all the capital letters to small letters and vice versa in C programming. To know more about string methods click on the string methods lesson.
Q7) Write a program in C to input a sentence and convert all the capital letters to small letters and vice versa.
Program
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char s[100];
int i;
printf("Enter a sentence\n");
gets(s);
for(i=0; i<strlen(s); i++)
{
// check if the character is an alphabet
if(isalpha(s[i])!=0)
{
if(isupper(s[i])!=0)
{
// change the character to lowercase if uppercase
s[i]=tolower(s[i]);
}
else if(islower(s[i])!=0)
{
// change the character to uppercase if lowercase
s[i]=toupper(s[i]);
}
}
}
//printing the character array
printf("%s",s);
return 0;
}
Output
Enter a sentence We arE LiVinG in A DemocraTiC CounTRY wE ARe lIvINg IN a dEMOCRAtIc cOUNtry