C program to remove spaces from a given string

In this article, you will learn and get code to remove spaces from strings using a C program. Here is the list of programs available in this article:

  1. Remove all spaces from a string entered by the user.
  2. Remove only extra spaces from a string entered by the user.

The first program removes all the spaces from a given string in C. For example, if the given string is

Welcome To     fresherearth

Then the string after removing all the spaces will be:

WelcomeTofresherearth

whereas the second program removes only extra spaces from the given string. For example, if the string entered by the user is:

    this    is       fresherearth

Then, after removing only extra spaces from this string, the new string will be:

this is fresherearth

Remove all spaces from the string in C

To remove or delete spaces from a string in C programming, you have to ask the user to enter a string. Now start checking for spaces. If space gets found at any index, then just shift all the forward characters one index back, as shown in the program given below. The question is, "Write a program in C that removes all the spaces from a given string by the user at run-time." The answer to this question is given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    char str[50];
    int i=0, j, chk;
    printf("Enter a String: ");
    gets(str);
    while(str[i]!='\0')
    {
        chk=0;
        if(str[i]==' ')
        {
            j=i;
            while(str[j-1]!='\0')
            {
                str[j] = str[j+1];
                j++;
            }
            chk = 1;
        }
        if(chk==0)
            i++;
    }
    printf("\nString (without spaces): %s", str);
    getch();
    return 0;
}

This program was built and runs in the Code::Blocks IDE. Here is its sample run:

c program remove spaces from string

Now supply any string and press the ENTER key to see the same string without a space. Here is the second snapshot of the sample run:

c remove spaces from string

Program Explained

Now let's discuss some of the main steps used in the above program with an example of the string shown in the previous sample run's snapshot:

Remove only extra spaces from a given string in C

Now this program only removes extra spaces from the string. That is, if there are two or more spaces available between any two words in the string, then after executing this program, there will only be one space left between all words.

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
    char str[200];
    int i, j, len;
    printf("Enter a String: ");
    gets(str);
    len = strlen(str);
    for(i=0; i<len; i++)
    {
        if(str[0]==' ')
        {
            for(i=0; i<(len-1); i++)
                str[i] = str[i+1];
            str[i] = '\0';
            len--;
            i = -1;
            continue;
        }
        if(str[i]==' ' && str[i+1]==' ')
        {
            for(j=i; j<(len-1); j++)
            {
                str[j] = str[j+1];
            }
            str[j] = '\0';
            len--;
            i--;
        }
    }
    printf("\nNew String = %s", str);
    getch();
    return 0;
}

Here is its sample run:

remove extra spaces from string c

Let me create the same program once more with a different approach:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[100], ch, count=0, len, i, j, temp;
    printf("Enter any string: ");
    gets(str);
    len = strlen(str);
    for(i=0; i<len; i++)
    {
        if(str[i]==32 && str[i+1]==32)
        {
            temp = i;
            for(j=i; j<(len-1); j++)
            {
                str[j] = str[j+1];
            }
            len--;
            str[len]='\0';
            i = 0;
        }
    }
    printf("The new string after removing all extra spaces:\n");
    puts(str);
    getch();
    return 0;
}

The above program was built and run in the Code::Blocks IDE, therefore here is the first snapshot of the sample run:

c remove extra spaces from string

Provide any string with extra spaces and press the ENTER key to see the output as shown here in the second snapshot of the sample run:

remove extra spaces c

Here are some of the main steps used in the above program:

The same program in different languages

C Quiz


« Previous Program Next Program »