Character replacements in a string – CSKC
Sign In

Search

Solved Problem #CPP#2673    siteicon   siteicon  

Problem Statement - Character replacements in a string

Write a c++ program to collect a small sentence from the user and then print the same sentence with all spaces replaced by hyphen (dash) character and all alphabets are replaced by their lower case counterparts.

Solution

TC++ #2673

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

#include <ctype.h> //This header is required to use function tolower() in this program

char sen[80]; cout<<“Input a small sentence:” ; cin.getline(sen,80);
User input sentence is collected in the character array.

for(int i=0;sen[i]!=”;i++) //Whole sentence is traversed till the null termination point
{
sen[i]=tolower(sen[i]);
Every character is converted to lowercase, this function automatically works on corresponding upper case character only so there is no need to check for character type.

if(sen[i]==’ ‘) sen[i]=’-‘;
If space character is found it is replaced by the hyphen (-) character.
}
cout<<“The converted sentence is now = ” <<sen<<endl;
Final string is now printed.

Notes

  • Modern compilers prefer to use the library <cctype> instead of <ctype.h> for compatibility to unicode and all new language enhancements.


Share

Created: 16-Jan-2018 22:20 | Updated: 17-Jan-2018 03:22|