×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous 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

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.

Problem statement

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

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?

C String Programs »

Advertisement
Advertisement

Related Programs

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.