0

I'm new to programing and i have to make a program in which you enter a sentence and remove any repeating characters like space, commas and so on. So my idea was to print only the characters that match my criteria. But i get a problem like this :

input : "This is(5 spaces for example) a (2 spaces) sample (3spaces)sentence".

but the output is :"This",instead of "This is a sample sentence"

My code is:

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

using namespace std;

int const l=200;
int main ()
{
    char a[l];
    cin >> a;

    int d;

    d=strlen (a);

    int i = 0;
    for(i = 0; i < d; i++)
    {
        if(a[i] != ' ' && a[i+1] != ' ')
        {
            cout<<a[i];
        }
    }
}

It would be nice if you tell me what my mistake is and how to solve it.Thanks in advance.

Sebastian
  • 8,046
  • 2
  • 34
  • 58
user3144334
  • 135
  • 1
  • 1
  • 9
  • 9
    Use `#include ` and [std::string](http://en.cppreference.com/w/cpp/string/basic_string) – Basile Starynkevitch Dec 29 '13 at 17:21
  • 2
    ...Aaaaaand indent your code. And use `std::size_t` for sizes. –  Dec 29 '13 at 17:23
  • 4
    BTW, your problem is that `std::istream::operator>>` only inputs until it encounters some whitespace. Don't use it. Use `std::cin.getline()` instead. –  Dec 29 '13 at 17:25

5 Answers5

1

Using cin will consider each space separated word as a single input and it will store only this as a single string is provided. Use cin.getline() instead of cin to get input with spaces.

char a[l];
cin.getline(a, l);
rullof
  • 7,124
  • 6
  • 27
  • 36
0

The problem lies in this condition here:

 if(a[i]!=' ' && a[i+1]!=' ')

When the program reaches the last space (which is the one you want to keep), it will fail the a[i] != ' ' condition and skip the character. What you want is:

 !(a[i] == ' ' && a[i+1] == ' ')
Brad
  • 2,261
  • 3
  • 22
  • 32
0

I have slightly modified your code for removing running spaces . You can edit for removing commas also. Use std:: string and getline function for reading strings as >> reads only till a space. I hope this helps.

#include<cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int const l=200;
int main() {
        string a;
        getline(cin,a);
        int d;
        d=a.size();
        int i = 0;
        for(i = 0; i < d; i++) {
                if(a[i]==' ') {
                        cout<<" ";
                        while(a[i]==' ')
                                ++i;
                }
                cout<<a[i];
        }
        cout<<endl;
}
alDiablo
  • 969
  • 7
  • 22
  • Is that working? modifiying i inside the while inside the for loop does not seem a good practice for me. – Netwave Dec 29 '13 at 17:42
  • It does work. In,fact modifying i makes it efficient as the running spaces are skipped and are not iterated twice. As soon as a[i] is not a space which is the end of the while loop , I print the character which ensures that none of the characters are missed. – alDiablo Dec 29 '13 at 17:47
  • 'while(a[i]==' ') ++i' is wrong –  Dec 29 '13 at 18:03
0

Lets see

#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string a;
    getline(cin, a);
    for(int i=0;i < a.length();i++)
    {
        if( i+1 < a.length())
        {
            bool whatIwant = !(a[i] == ' ' && a[i+1] == ' ');
            if(whatIwant){ cout<<a[i]; }
        }
        else { cout<<a[i]; }
    }
}

Not the better or efficiente way of doing it, but, this should do the job. Tested, and working.

Notice that if not catching the i+1 condition properly you coult jump into memory trouble.

Netwave
  • 40,134
  • 6
  • 50
  • 93
0

There are two problems.

The first one is that you use operator >> for entering the sentence. This operator enters only symbols beofre a white space. So when you enter

"This is a sample sentence"

the result of applying operator

cin >> a;

will be that array a will contain only word "This". Instead of this operator you should use function

cin.get( a, l );

The second problem is that the condition in the if statement is incorrect. Should be

    if ( a[i] != ' ' || a[i+1] != ' ' )
    {
        cout<<a[i];
    }

Also you can use standard algorithm std::unique_copy. For example

    std::unique_copy( a, a + std::strlen( s ), std::ostream_iterator<char>( std::cout ),
        []( char c1, char c2 ) { return ( c1 == ' ' && c1 == c2 ); } );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335