C Program to convert first character uppercase in a sentence


Given a string and with mixed case, i.e with both uppercase and lower case, the task is to covert the first character to uppercase rest in lowercase if it’s in upper case.

Let’s understand it in depth with the help of a simple example.

Like we are given a string “hElLo world”, we have to convert the first character ‘h’ which is in lowercase to uppercase ‘H’ and rest all letters before the space or end of the string to lowercase.

Moreover when we encounter first character after a space we have to convert it to uppercase.

Example

Input: str[] = {“heLlO wORLD”}
Output: Hello World
Input: str[] = {"sUNIDHi bAnSAL"}
Output: Sunidhi Bansal

Approach used below is as follows

  • To solve this problem we will use the ASCII values of characters.
  • ASCII stands for American Standards Code for Information Interchange, implies that every character has some code associated to it. Like uppercase letters A-Z are given numerical values from 65-90 and lowercase a-z are given numbers ranging 97-122. So we will be using these values to solve our problem.
  • We will traverse the whole string and,
  • Check if the first character or the immediate character after a space ‘’ is in range a-z then we will subtract the ASCII values of 'A' and 'a' then add the result to the character.
  • If it is not the first character or the character immediate after the space then we have to check whether the character is in uppercase then convert to lowercase add the character’s value to the result of subtraction of 'a' - 'A'.

Algorithm

Start
In Function int firstupper(char str[], int n)
   Step 1-> Initialize int i
   Step 2-> Loop For i = 0 And i<n And i++
      If i == 0 && str[i] != ' ' || str[i] != ' ' && str[i-1] == ' ' then,
         If str[i] >= 'a' && str[i]<='z' then,
            str[i] = (char)(('A'-'a') + str[i] )
         End If
         Else If str[i] >= 'A' && str[i] <= 'Z' then,
            str[i] = (char)(str[i] + ('a' - 'A'))
         End If
      End Function
In Function int main(int argc, char const *argv[])
   Step 1-> Declare and initialize char str[] = {"sUNIDHi bAnSAL"}
   Step 2-> Declare n set as sizeof(str)/sizeof(str[0])
   Step 3-> firstupper(str, n)
   Step 4-> Print "%s
", str End main

Example

 Live Demo

#include <stdio.h>
int firstupper(char str[], int n) {
   int i;
   for(i = 0; i<n; i++) {
      if (i == 0 && str[i] != ' ' || str[i] != ' ' && str[i-1] == ' ') {
         if(str[i] >= 'a' && str[i]<='z') {
            str[i] = (char)(('A'-'a') + str[i] );
         }
      } else if (str[i] >= 'A' && str[i] <= 'Z') {
         str[i] = (char)(str[i] + ('a' - 'A'));
      }
   }
   return 0;
}
int main(int argc, char const *argv[]) {
   char str[] = {"sUNIDHi bAnSAL"};
   int n = sizeof(str)/sizeof(str[0]);
   firstupper(str, n);
   printf("%s
", str);    return 0; }

Output

Sunidhi Bansal

Updated on: 18-Oct-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements