codescracker


c

C Program to Remove Spaces from String



« Previous Program Next Program »


Remove Spaces from String in C

To remove or delete spaces from the string or sentence in C programming, you have to ask to the user to enter a string. Now start checking for spaces. If space will found, then start placing the next character from the space to back until the last character and so on.

C Programming Code to Remove Spaces from String

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<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
	clrscr();
	char str[80];
	int i=0, len, j;
	printf("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--;
		}
	}
	printf("String after removing spaces = %s", 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 Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test