Q:

C program to convert a string to sentence case

0

C program to convert a string to sentence case

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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?

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now