Write a C program that reads in one line of text from the Standard input, and writes out the line with the following changes:
The first letter of all sentences in uppercase (capitalize the first letter).
There are two spaces between the end of one sentence and the following sentence (except the first sentence).
A sentence is always terminated by either a full stop (.), a question mark (?) or an exclamation mark (!), and then followed by zero or more spaces.
Code:
Enter a line for processing:
abc. abc? abc! abc?! abc.abc, abc
Abc. Abc? Abc! Abc?! Abc. Abc, abc
This is my work out, does any line in my program is having mistake or any things need to change to make the program ore effiency?
Code:
#include <stdio.h>
#include <ctype.h>
#define size 160
void main() {
char charArray[size];
int i;
char firstupper;
printf("Enter a line for processing:\n");
scanf("%[^\n]",charArray);
firstupper=toupper(charArray[0]); /* Change first char to uppercase*/
printf("%c", firstupper); /* Print first char after uppercase*/
for (i=1; i<=size; i++) {
if (charArray[i]=='\0') /* Stop if \n */
break;
if ( (charArray[i]=='.' && charArray[i+1]==' ') ||
(charArray[i]=='!' && charArray[i+1]==' ') ||
(charArray[i]=='?' && charArray[i+1]==' ') ) /* Check char='! ','? ','. ' or '!','?','.' following by space */
{ printf("%c ",charArray[i]);
i=i+2;
printf("%c", toupper(charArray[i]));}
else if ((charArray[i]=='.') || (charArray[i]=='!') || (charArray[i]=='?'))
{ printf("%c ",charArray[i]);
i=i+1;
printf("%c", toupper(charArray[i]));}
else
printf("%c", (charArray[i]));
}
fflush(stdin);
getchar();
}