QF Marine
QF Marine

Reputation: 21

Stuck on removing whitespace from string without using any helper code c++

Create a program titled str_compress.cpp. This program will take a sentence input and remove all spaces from the sentence. (A good first step in encryption programs) Make sure that both the input and output strings are all stored in a single variable each. Do not use numbers or symbols. Include both upper-case and lower-case letters. Account for cases with multiple spaces anywhere.

This is what I have so far:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int i = 0, j = 0, len;
    string str;

    cout << "Enter string: ";
    getline(cin, str);

    len = str.length();

    for (i = 0; i < len; i++)
    {
        if (str[i] == ' ')
        {
            for (j = i; j < len; j++)
            {
                str[j] = str[j + 1];
            }
            len--;
        }
    }

    cout << str << endl;

    system("pause");
    return 0;
}

I can eliminate spaces, but only one at a time. If I copy and paste the for loop, I can remove all spaces for how many loops there are. I'm thinking that I can loop the for loop over and over until all spaces are gone, but I'm not sure how to do that. Also, I can't use anything like remove_all() or erase().

Upvotes: 2

Views: 104

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 595432

Your loop's logic when removing a space is wrong. For instance, after removing a space, you then skip the next char in the string, which may be another space. Also, although you are decrementing the len, you don't resize the string to the new len before printing the new str value.

It should look more like this:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    size_t i, j, len;
    string str;

    cout << "Enter string: ";
    getline(cin, str);

    len = str.length();

    i = 0;
    while (i < len)
    {
        if (str[i] == ' ')
        {
            for (j = i + 1; j < len; ++j)
            {
                str[j - 1] = str[j];
            }
            --len;
        }
        else
            ++i;
    }

    str.resize(len);
    cout << str << endl;

    /* or, if you are not allowed to use resize():
    cout.write(str.c_str(), len);
    cout << endl;
    */

    /* or, if you are not allowed to use write():
    if (len < str.length())
        str[len] = '\0';
    cout << str.c_str() << endl;
    */

    system("pause");
    return 0;
}

Live Demo

However, your instructions do say to "Make sure that both the input and output strings are all stored in a single variable each", which implies that separate std::string variables should be used for input and output, eg:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    size_t i, j, len;
    string str, str2;

    cout << "Enter string: ";
    getline(cin, str);

    str2 = str;
    len = str2.length();

    i = 0;
    while (i < len)
    {
        if (str2[i] == ' ')
        {
            for (j = i + 1; j < len; ++j)
            {
                str2[j - 1] = str2[j];
            }
            --len;
        }
        else
            ++i;
    }

    str2.resize(len);
    cout << str2 << endl;

    /* or:
    cout.write(str2.c_str(), len);
    cout << endl;
    */

    /* or:
    if (len < str2.length())
        str2[len] = '\0';
    cout << str2.c_str() << endl;
    */

    system("pause");
    return 0;
}

Live Demo

Alternatively:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    size_t i, j, len;
    string str, str2;

    cout << "Enter string: ";
    getline(cin, str);

    len = str.length();
    str2.reserve(len);

    for(i = 0; i < len; ++i)
    {
        char ch = str[i];
        if (ch != ' ')
            str2 += ch;
    }

    cout << str2 << endl;

    system("pause");
    return 0;
}

Live Demo

Upvotes: 3

QF Marine
QF Marine

Reputation: 21

This is what worked for me. Thank you everyone for the help!!

int main()
{
int i, j, len;
string str, str2;

cout << "Enter string: ";
getline(cin, str);

len = str.length();

for (i = 0; i < len; ++i)
{
    char ch = str[i];
    if (ch != ' ')
        str2 += ch;
}

cout << str2 << endl;

system("pause");
return 0;
}

Upvotes: 0

anatolyg
anatolyg

Reputation: 28241

This is a strong clue for how the authors of your exercise want you to write your code:

Make sure that both the input and output strings are all stored in a single variable each

You should make a new string:

string new_str;

Use your loop over the input string. For each char in the string, check whether it is a space. If yes, do nothing. If no, append it to the output string:

for (i = ...)
{
    char c = str[i];
    if (c != ' ')
        new_str.push_back(c);
}

Upvotes: 4

Related Questions