ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C program to convert a string to sentence case

In this C program, we are going to learn how to convert a given string to sentence case? To implement this logic, we are creating a user define function to do the same.
Submitted by IncludeHelp, on April 05, 2018

Given a string and we have to convert it to sentence case using C program.

So, first of all, we have to understand - what is a sentence case? A string is in a sentence case, if first character of each sentence is an uppercase character.

Example:

    Input string: "hello how are you?"
    Output:
    Sentence case string is: "Hello how are you?"

    Input string: "hello. how are you?"
    Output:
    Sentence case string is: "Hello. How are you?"

Program to convert string to sentence case in C

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

// function to convert string to sentence case
void StrToSentence(char * string)
{
	int length=0,i=0;

	length = strlen(string);

	for(i=0;i<length;i++)
	{
		if( (i==0) && (string[i]>='a' && string[i]<='z'))
		{
			string[i] = string[i] - 32;
		}
		else if(string[i]=='.')
		{
			if(string[i+1] == ' ')
			{
				if(string[i+2]>='a' && string[i+2]<='z')
				{
					string[i+2] = string[i+2] - 32;
				}
			}
			else
			{         
				if(string[i+1]>='a' && string[i+1]<='z')
				{
					string[i+1] = string[i+1] - 32;
				}
			}
		}
	}
}

// main function
int main()
{
	char string[50]={0};
	int length=0,i=0,j=0,k=0;

	printf("\nEnter the string : ");
	gets(string);

	// pass the string to the function
	StrToSentence(string);

	printf("Final string is : %s",string);
	return 0;
}

Output

Run 1: 
Enter the string : hello world. how are you?
Final string is : Hello world. How are you?

Run 2:
Enter the string : hello.how are you? 
Final string is : Hello.How are you?

C String Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.