C/C++ Program to Remove Spaces From String

C Program-

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

C++ Program-

#include<iostream>
#include<stdio.h>
 
using namespace std;
 
int main()
{
	int i,j=0;
	char str[30];
	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-

Enter a String:
i am a gamer
String After Removing Spaces:
iamagamer

Leave a Comment

error: Alert: Content is protected!!