C Program To Add Spaces In A String

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

  1. C Program To Add Digits Of A Number
  2. C Program To Find Leap Year
  3. C Program To Append Data Into A File
  4. C Program to Accept Only Integer Values
  5. C Program To Arrange Numbers In Descending Order
  6. C Program to Add Days to Date
  7. C Program to Add Two Fractions
  8. C Program To Reverse A Number
  9. C Program to Find Maximum and Minimum Numbers
  10. C Program to Read a Text File
  11. C Program to Convert Decimal to Hexadecimal
  12. C Program to Convert Decimal to Binary
  13. C Program to Convert Celsius to Fahrenheit
  14. C Program To Find Absolute Value
  15. Ternary Operator Program in C
  16. C Program For Addition Table Using For Loop
  17. Denomination Program in C
  18. C Program to Print Multiplication Table 
  19. C Program Array with Example
  20. Binary Search in C Program Using Recursion

Leave a Comment