C Program To Add Spaces In A String. If you are looking for C program to add space to a string, here in this tutorial we will help you to learn how to write a C program to add space to a string.
C Program to Add Spaces in a String
Learn how to write a C program to add spaces in a string. Writing C Program to add space to a string can be done using various techniques but here in this program, we show how to write a C program to adding spaces in a string in a proper way.
C Program to Add Spaces in a String Source Code
/* C Program to add spaces in a string - AddSpacesInString.C */ #include <iostream> //#include <iomanip> #include <cctype> #include <string> using namespace std; //void fixPuct(string); void fixPuct(string&); int main() { //char *statement; // don't mix C strings with C++ //statement = new char [60]; cout << "Enter a statement with Caps at start of each new word ..." << endl; // Enter a statement with Caps at start of each new word ... string statement; // construct an empty C++ string if( getline(cin, statement) ) //if (statement != NULL) { //cout << "Enter a statement: "; //cin. getline(statement, 60);// see above re. using C++ strings with C++ //getline( cin statement ); // see above re. using C++ strings with C++ cout << "You entered " << '"' << statement << '"' << endl; fixPuct(statement); cout << "Here it is fixed ...\n" << statement << endl; //delete [] statement; } cout << "\nPress 'Enter' to continue/exit ... " << flush; getline( cin, statement ); } //void fixPuct(string s) void fixPuct(string& s) // Note: NEED to pass in by reference so calling string gets updated ... { int size = s.size(); if(size) { string nLine(1, s[0]); for( int i=1; i<size; i++ ) { if( isupper(s[i]) ) { if( i > 0 && !isspace(s[i-1]) ) nLine += ' '; nLine += tolower(s[i]); } else nLine += s[i]; } s = nLine; } }
C Program to Add Spaces in a String Output
Enter a statement with Caps at start of each new word …
TheBig BrownBear sat OnTheLittleBlueBox.
Here it is fixed …
The big brown bear sat on the little blue box.
Press ‘Enter’ to continue/exit …
C PROGRAMMING EXAMPLES
- C Program To Add Digits Of A Number
- C Program To Find Leap Year
- C Program To Append Data Into A File
- C Program to Accept Only Integer Values
- C Program To Arrange Numbers In Descending Order
- C Program to Add Days to Date
- C Program to Add Two Fractions
- C Program To Reverse A Number
- C Program to Find Maximum and Minimum Numbers
- C Program to Read a Text File
- C Program to Convert Decimal to Hexadecimal
- C Program to Convert Decimal to Binary
- C Program to Convert Celsius to Fahrenheit
- C Program To Find Absolute Value
- Ternary Operator Program in C
- C Program For Addition Table Using For Loop
- Denomination Program in C
- C Program to Print Multiplication Table
- C Program Array with Example
- Binary Search in C Program Using Recursion