- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
- Related Articles
- C# Program to convert first character uppercase in a sentence
- Java Program to convert first character uppercase in a sentence
- C# Program to replace a character with asterisks in a sentence
- Program to reverse a sentence words stored as character array in C++
- Convert a String to Uppercase in C
- Count spaces, uppercase and lowercase in a sentence using C
- Swift Program to Convert a String to Uppercase
- C Program to read and write character string and sentence
- Write a C program to convert uppercase to lowercase letters without using string convert function
- C# Program to Convert Character case
- Java program to convert a string to lowercase and uppercase.
- Get field value and convert a particular character from it to uppercase with MySQL
- C++ program to convert kth character to lowercase
- PHP program to find the first word of a sentence
- Java program to swap first and last characters of words in a sentence

Advertisements