C++ Program to Remove Spaces From String

C++ Program to Remove Spaces From String:The program takes a string and removes the spaces in it. Given a string, remove all spaces from it. For example “g e e k” should be converted to “geek” and ” g e ” should be converted to “ge”.

C++ Program to Remove Spaces From String

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int i,j=0;
	char str[40];
	cout<<"Enter a String:\n";
	gets(str);
	
	for(i=0;str[i]!='\0';++i)
	{
	if(str[i]!=' ')
	str[j++]=str[i];
	}
	
	str[j]='\0';
	
	cout<<"\nString After Removing Spaces:\n"<<str;
	return 0;
}

Output of Program

Enter a String:
My name is khan and Owner of CodeBlah

String After Removing Spaces:
MynameiskhanandOwnerofCodeBlah