0

I wrote a program which is supposed to remove excess spaces from a string. But it only shows characters before spaces. It finds a space and checks the character after that whether it is a space. Depending on excess spaces it shifts other characters over excess spaces. But output is very confusing.

input: "qwe(2 spaces)rt(one space)y"

output: "qwe(one space)rt(one space)y"

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

using namespace std;

int main(){
    string a;
    cin >> a;
    int len = a.length();
    int new_len=len;
    int z,s=0;
    for(int i=0; i<new_len; i++){
        if(a[i]==' '){
            z=i+1;
            s=0;
            //Assigning the number of excess spaces to s.
            while(a[z]==' '){
                s++;
                z++;
            }
            //doing the shifting here.
            if(s>0){
                for(int l=i+1; l<new_len-s; l++){
                    a[l]=a[s+l];
                }
            }
            new_len-=s;
        }

    }
    cout << a << endl;
    cout << a.length();
    system("pause");
    return 0;
}
4
  • Did you debug your code? Commented Mar 15, 2013 at 5:26
  • 2
    Is this what you're trying to do? stackoverflow.com/questions/8362094/… Commented Mar 15, 2013 at 5:26
  • I would consult std::string and consider doing it a little more efficiently using find_first_of() and find_first_not_of() and their simulars. Commented Mar 15, 2013 at 5:28
  • std::string a; cin >> a; skips leading whitespace and only reads data into a until it hits more whitespace (which isn't read into a) or end-of-file, so you can't possibly have input with any whitespace to strip. If you'd put cout << "a '" << a << "'\n"; into your program you'd have noticed: such "trace" output is an essential diagnostic for programming, and would let you watch your program work.... Commented Mar 15, 2013 at 5:32

3 Answers 3

1

Most of your code is semi-pointless -- when you use the normal string extractor (stream >> string) it automatically skips across all consecutive leading white-space, and stops reading at the first whitespace character. As such, it's already doing almost everything the rest of your code is intended to accomplish. That leaves a much simpler approach to accomplishing the same task:

std::copy(std::istream_iterator<std::string>(std::cin),
          std::istream_iterator<std::string>(),
          std::ostream_iterator<std::string>(std::cout, " "));

This does have one problem: it'll leave one extra space at the end of the output. If you don't want that, you can use the infix_ostream_iterator I've posted previously. With that, you'd change the above to something like this:

std::copy(std::istream_iterator<std::string>(std::cin),
          std::istream_iterator<std::string>(),
          infix_ostream_iterator<std::string>(std::cout, " "));
Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand this code. But it might be the solution.
1

If you're using C++11 doing this your way is overkill - you can just use a regex. Something like the following should do it (untested):

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

int main(){
  string a;
  cin >> a;
  regex r(" +");
  a = regex_replace(a,r," ");
  cout << a << endl;
  cout << a.length();
  system("pause");
  return 0;
}

Comments

1

Your code is highly ineffective. Imagine a following string with 1,000,000 characters:

a  a  a  a  a  a  a...

Each time your algorithm encounters a second space, it goes through the whole string to shift it one char left. I would attempt another approach:

  • Create two iterators, like realPos and charPos. Set them to 0 at the beginning.
  • Create a variable, which stores a number of spaces encountered so far, like spacesSeen. Set it to 0.
  • Now, while realPos is lower than length of the whole string:
    • If string[realPos] != ' ' and charPos != realPos, make an assignment: string[charPos] = string[realPos]. Then increase both realPos and charPos by one. Set spacesSeen to 0.
    • If string[realPos] == ' ' and spacesSeen == 0, increase spacesSeen by one, copy characters and advance both iterators.
    • If string[realPos] == ' ' and spacesSeen > 0, then increase spacesSeen and then increase only realPos.
  • Now charPos marks the place where your final string ends, adjust strings size, such that it ends there.

In simpler words: copy the characters one by one and skip multiple spaces on the way.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.