CAPITALIZE LETTER

PROBLEM LINK:
[CAPITALIZE LETTER] (CodeChef: Practical coding for everyone)

PRE-REQUISITES
C Programming Language

PROBLEM

PROBLEM STATEMENT:

Given an info line, underwrite first and last letter of each word by altering into uppercase in the given line . Generate the output after alteration with the same input line given . When given that the information line is in lowercase. .

SOLUTION:

The basic algorithm is to keep track of the spaces and to capitalize the letter before space & after space. Also, the first letter and the last letter of the line should be capitalized. Few more things that need to be kept in mind such that:

  1. More than one occurrence of space in between two words.
  2. There may be word of single letter like ‘k’, which need to be capitalized.
  3. There may be word of two letters like ‘ok’, where both the letters need to be capitalized.

INPUT:

kagpil atgnth vatyu

OUTPUT:

KagpiL AtgntH VatyU

SOLUTION:

#include <stdio.h>
#include <string.h>

int main()
{

char str[100];
int size= 0;

gets(str);

size= strlen(str);

for(int i=0;i<size;i++)
{
if(i==0||i==(size-1)) //
{
str[i]=toupper(str[i]);
}
else if(str[i]==’ ')
{
str[i-1]=toupper(str[i-1]);
str[i+1]=toupper(str[i+1]);

  }

}
printf(“%s”, str);

return 0;
}