Home » C solved programs » C string programs

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


#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?





Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





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 (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.