C/C++ Program to Remove Spaces From String

For instance, the given string is “the insane software engineer”. Presently we need to expel every one of the spaces present in the string.

Previously: Justtech Review

After: tJusttechReview

Underneath I have shared a program that performs above undertaking with straightforward methodology. You can ask your questions in the remark area.

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 programmer
String After Removing Spaces:
iamprogrammer

Leave a Comment

error: Alert: Content is protected!!