codescracker


c++

C++ Program to Remove Spaces from String or Sentence



« Previous Program Next Program »


Remove Spaces from String in C++

To remove or delete spaces from the string or sentence, you have to ask to the user to enter a string. Now start checking for spaces. If space will be found, then start placing the next character from the space to the back until the last character and continue to check for next space to remove all the spaces present in the string as shown here in the following program.

C++ Programming Code to Remove or Delete Spaces from String or Sentence

Following C++ program ask to the user to enter any sentence or string to remove spaces from the sentence or string, then display the string or sentence after removing the spaces:

/* C++ Program - Remove Spaces from String */
		
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
	clrscr();
	char str[80];
	int i=0, len, j;
	cout<<"Enter the String : ";
	gets(str);
	len=strlen(str);
	for(i=0; i<len; i++)
	{
		if(str[i]==' ')
		{
			for(j=i; j<len; j++)
			{
				str[j]=str[j+1];
			}
		len--;
		}
	}
	cout<<"String after removing spaces = "<<str;
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program remove spaces from string

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test