Print statement without spaces.

Apr 13, 2013 at 12:29am
Hello, i am trying to write a program to print a statement without spaces in it.
For example, if the statement is "Hello, i am Solidsnake", then it should print it as "Hello,iamsolidsnake". Can someone help me?
Apr 13, 2013 at 1:40am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <iostream>
#include <algorithm>

std::string stripit_stripitrealgood(std::string s)
{
    s.erase( std::remove_if(s.begin(), s.end(), [](char ch) { return ch == ' ';}), s.end() ) ;
    return std::move(s) ;
}

int main()
{
    std::string str ;

    std::cout << "Input: " ;
    std::getline(std::cin, str) ;

    std::cout << stripit_stripitrealgood(str) << '\n' ;
}
Last edited on Apr 13, 2013 at 1:41am
Apr 14, 2013 at 5:49pm
I found the answer:
#include <iostream>
using namespace std;

int main()
{

string word;
getline(cin,word);
cout<<"You have entered :"<<word<<endl;
for(int i =0;i<word.length();i++)
{
if(word[i]==' ')
{
continue;
}
cout<<word[i];
}
}
Apr 14, 2013 at 6:54pm
Very good. Make sure to use indenting (and around here, [code] tags too).

@cire I think it unlikely that OPs professor expects OP to use the STL like that. Prof would like OP to figure out how to iterate over array and examine elements and take action (or inaction) as appropriate.
Apr 14, 2013 at 8:30pm
@Duoas:
Thanks for the tips. Will follow them in the future. Closing the thread.
Topic archived. No new replies allowed.