How to remove or eliminate the extra spaces from a given string in C++

In this tutorial, we are going to eliminate or remove the extra spaces from a given string in C++. It is a basic question which is based on the concept of arrays. This technique is very important and has its applications in different domains.

The concept behind this program is useful in HTML and accepting data from web pages. Many of the time users enter spaces more than once which is not acceptable, also it creates many complications. So the necessity to remove extra spaces comes into action.

If you are searching for the same the solution to similar problem then you are at right place.

Eliminate or Remove the extra spaces from a given string in C++

Methodology

The declaration of all the variables will be our first step for coding. The string value will be stored in a variable “str“. Then a new empty string variable will be created “newstr“. A¬† variable “index” is also defined which we will later use for indexing purpose of our new string value.

int check=0;
char newstr[80];            //new string
int index=0;                //for indexing the new string

//string from which spaces are to be removed
char str[]="This   program  remove   extra spaces from  string";

The main codes lines which remove the extra spaces are below
The variable “check” is used to ensure multiple spaces are removed and retain only one space between two words in string.



while(str[i]){                          //while loop runs till end of string
    c=str[i];
    if(isspace(c)&&check==0){           //check spaces
        newstr[index++]=str[i];
        check=1;
    }else if(isalnum(c)){                //check alphabets and numbers
        newstr[index++]=str[i];
        check=0;
    }
    i++;
}

Each character of the string will be stored in character “c“. Then it is checked for different conditions.

  • Alphanumeric character is stored as it is in “newstr“.
  • Space is also stored as it is only for the first time between two words because it is required only once here.
  • If the space is for the more than once, then it is ignored and this condition is accomplished by using “check” variable.

The value of the variable “check” ensures correct spacing. the value 0 indicated that the space occurs for the first time in particular words pair, for second or later times the value of check becomes 1. By its value, the program decides whether to store the particular space or not.

Final Program

I have written this code in Code Blocks v 16.01 and this is working fine, it will also work on any other C++ IDE without any issue.

#include <iostream>
#include<string.h>

using namespace std;

int main()
{
    int check=0;
    char newstr[80];            //new string
    char c;
    int index=0;                //for indexing the new string
    int i=0;

    //string from which spaces are to be removed
    char str[]="I    am a    string with     too   many   extra spaces";    // change this string

    while(str[i]){                          //while loop runs till end of string
        c=str[i];
        if(isspace(c)&&check==0){           //check spaces
            newstr[index++]=str[i];
            check=1;
        }else if(isalnum(c)){                //check alphabets and numbers
            newstr[index++]=str[i];
            check=0;
        }
        i++;
    }
    cout<<"Initial value of string-"<<endl;      //old string
    cout<<str<<endl;
    cout<<endl;
    cout<<"Final value of string-"<<endl;        //changed string after removing extra spaces
    cout<<newstr<<endl;
    return 0;
}

Output

Initial value of string-
I    am a    string with     too   many   extra spaces

Final value of string-
I am a string with too many extra spaces

Hope you are now able to implement the code in program of your choice.

Also read

C++ program to create a text file, open and read a particular line

Leave a Reply

Your email address will not be published. Required fields are marked *