-5

just want to know how I can get a string from stdin which contains whitespaces? I tried fgets and scanf("%[^\n]",str) but it's still not working in C.

I tried the program to remove whitespaces from a given string in c++. Here's is my code but it's not working.

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

int main() {
    // your code goes here
    int t;
    cin >> t;
    while (t--) {
        char s[1000];
        cin.getline(s, 1000);
        // cout<<s;
        int i;
        for (i = 0; s[i]; i++) {
            if (s[i] != ' ')
                s[i] = '\b';
        }
        for (i = 0; s[i]; i++)
            cout << s[i];
        // cout<<endl;
    }
    return 0;
}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
shivendu
  • 3
  • 2
  • Why are you replacing every non-space character with `'\b'`? – Benjamin Lindley May 01 '16 at 04:28
  • `getline()` might work for you – Stephen May 01 '16 at 04:29
  • 5
    It is confusing to show a C++ program in a question about C. You should really have two questions — two separate questions. One should be about why your C++ isn't working. The other should show your best effort at the C program and explain why it is not working. You should be able to make `fgets()` work to read in lines regardless of the white space within the line. – Jonathan Leffler May 01 '16 at 05:30
  • don't mix c and c++. Use C++ input methods. – lxx May 01 '16 at 05:50
  • Thanks for your help, actually i was confused between c and c++. Now got it !!! – shivendu May 05 '16 at 16:27

2 Answers2

1
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string getInput( string input )
{
    getline( cin, input );

    return input;
}

// Handles tabs and spaces
string removeWhitespace( string input )
{
    input.erase( remove_if( input.begin(),
                            input.end(),
                            []( char ch ){ return isspace( ch ); } ),
                 input.end() );

    return input;
}

int main()
{
    cout << removeWhitespace( getInput( {} ) ) << endl;

    return 0;
}
davepmiller
  • 2,620
  • 3
  • 33
  • 61
0

Your code is already reading lines with spaces. That's what getline does. Strangely though, you have this loop here

for (i = 0; s[i]; i++) {
  if (s[i] != ' ')
    s[i] = '\b';
}

That's going to replace all the visible characters with '\b', which is the backspace character, and is not visible in most terminals. If you delete that loop, your code works almost right. The only remaining problem is that for the first iteration of your loop, you will not be able to input anything, because of this line:

cin >> t;

A trailing newline character will remain in the input buffer before the first call to getline. That problem is explained in the answers to this question: cin.getline() is skipping an input in C++ -- and many many duplicates. However, even if you don't fix that, after the first line, getline should be reading lines properly as is.

Community
  • 1
  • 1
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274