strings

May 25, 2013 at 2:08am
Q1:-The user enters a sentence your task is to output every word on a new line.
*please note that i haven't learnt strings yet so please help me with it.*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include<iostream>
#include<string>
using namespace std;
int main(){

    cout<<"enter your sentence"<<endl;
    string a;
    getline(cin,a);

 int b=a.length();
    for(int i=0;i<b;i++){

       if(a==" ")
        cout<<endl;
        else
           cout<<a.substr(i,b);
    }
return 0;}
May 25, 2013 at 2:46am
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main()
{
    while (std::cin.peek() != '\n')
    {
        std::string word ;
        std::cin >> word ;
        std::cout << word << '\n' ;
    }
}


Of course, you can fool that piece of code by not ending a sentence with a newline, for instance by entering a space then a newline at the termination of a sentence, thereby causing the loop to continue past the point where it should stop.

Making a helper function or two to advance to the next word in the sentence might help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <cctype>

bool nextIsWhitespace(std::istream& is)
{
    int ch = is.peek() ;

    if ( ch == std::char_traits<std::istream::char_type>::eof() )
    {
        is.get(); // set eof flag
        return false ;
    }

    return std::isspace(static_cast<unsigned char>(ch)) ;
}

bool nextWord(std::istream& is)
{
    while ( nextIsWhitespace(is) && is.peek() != '\n' )
        is.ignore() ;

    bool isNextWord = is && is.peek() != '\n' ;

    if ( !isNextWord )
        is.ignore() ;

    return isNextWord ;
}

int main()
{
    do
    {
        std::string word ;
        std::cin >> word ;
        std::cout << word << '\n' ;
    } while ( nextWord(std::cin) ) ;
}


Alternately, one might take advantage of stringstreams:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::cout << "Enter a sentence.\n" ;
    std::string sentence ;
    std::getline(std::cin, sentence) ;

    std::istringstream wordstream(sentence) ;

    std::string word ;
    while ( wordstream >> word )
        std::cout << word << '\n' ;
}


Come to think of it, there may be a few more ways to do this.
May 25, 2013 at 2:50am
One easy way is to replace white spaces with new lines. Note that if you have space space, then the output will contain an empty line

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<string>

using namespace std;
int main()
{
    cout<<"enter your sentence"<<endl;
    string a;
    getline(cin,a);

    for(int i=0;i<a.length();i++)
   {
       if(a[i]==" ") a[i]='\n';
    }
   cout<<a<<endl;
   return 0;
}

May 25, 2013 at 2:57am
@ats15 the code doesn't work it says that you can't compare integer and pointers in if loop. what should i do?
May 25, 2013 at 3:01am
You might change line 13 to read:

if (a[i]==' ')...
May 25, 2013 at 3:08am
thank you it makes sense now...:)
Topic archived. No new replies allowed.