Input a sentence and change the first alphabet to capital and rest to small letters in C
String Methods - Question 10
In this question, we will see how to input a sentence and change the first alphabet of every word to capital and rest to small letters in C programming. To know more about string methods click on the string methods lesson.
Q10) Write a program in C to input a sentence and change the first alphabet of every word to capital and rest to small letters. Assume that input may be in any case.
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);
strlwr(s);
for(i=0; i<strlen(s); i++)
{
if(i==0 && s[i]!=' ')
{
s[i]=toupper(s[i]);
}
else if(s[i]==' ' && s[i+1]!=' ')
{
s[i+1]=toupper(s[i+1]);
}
}
//printing the character array
printf("%s",s);
return 0;
}
Output
Enter a sentence a quiCk bRown fox juMP oVeR the LAZy DOG A Quick Brown Fox Jump Over The Lazy Dog