C Program to Remove Spaces from String

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

  1. Remove all Spaces from a String entered by User
  2. Remove only Extra Spaces from a String entered by User

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

Welcome To     CodesCracker

Then the string after removing all the spaces will be:

WelcomeToCodesCracker

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

    this    is       codescracker

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

this is codescracker

Remove All Spaces from String

To remove or delete spaces from string in C programming, you have to ask from user to enter a string. Now start checking for spaces. If space gets found at any index, then just shift all the forward character to 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 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 build and run under Code::Blocks IDE. Here is its sample run:

c program remove spaces from string

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

c remove spaces from string

Program Explained

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

  • The string gets stored in str variable in a way that
    • str[0] holds W
    • str[1] holds e
    • str[2] holds l
    • and so on
  • Because, spaces present is at index no. 7, 10, 11, 12, 13, 14. So
    • str[7], str[10], str[11], str[12], str[13], str[14] holds space
  • So when the value of i becomes 7, then the condition of first if block evaluates to be true. That is, space gets found at 7th index.
  • So the value of i gets initialized to j and the shifting of all forward characters (from where the space gets found) to one index back, gets performed. Because, currently the space gets found at index number 7. So
    • The character at index no.8 moved to index no.7
    • The character at index no.9 moved to index no.8
    • The character at index no.10 moved to index no.9
    • and so on
    • until the last character of the string
  • Because the value of i is 7 right now, and the character was at index no.8 gets moved back to the index no.7
  • Therefore here the value of i does not gets incremented, because for now the character present at index no. 7 is unknown
  • That is, we have to check it, whether it is a character or a space
  • And if no space gets found at current index, then just increment the value of i and check for character present at next index number
  • Continue the process, until the last character

Remove Extra Spaces from String

Now this program removes only 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 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

Same Program in Other Languages

C Online Test


« Previous Program Next Program »