codescracker
c

C Program to Remove Spaces from String



« Previous Program Next Program »

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:

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 »



© Copyright 2021. All Rights Reserved.

CodesCracker