943,852 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1028
  • C++ RSS
Mar 3rd, 2009
0

splitting sentance into words

Expand Post »
hey guys i want to slpit a sentence into words but the pogram i have written gives only the first word...can u help me plz.....

C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<dos.h>
  5. #include<stdio.h>
  6.  
  7. void main()
  8. {
  9. clrscr();
  10. char text[100];
  11. int i=0;
  12.  
  13. clrscr();
  14. cout<<"enter the sentence";
  15. cin.getline(text,100);
  16.  
  17. while(i<strlen(text))
  18. {
  19. char text1[]=" ";
  20. int m=0;
  21. while(text[i]!=' ')
  22. {
  23. text1[m]=text[i];
  24. m++ ;
  25. i++;
  26. }
  27. i++;
  28. cout<<"\n"<<text1;
  29. }
  30.  
  31. getch();
  32. }



if the input is

hello world

the output i want is

hello
world

but get only hello

thanks in advance
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunderthomas is offline Offline
3 posts
since Mar 2009
Mar 3rd, 2009
0

Re: splitting sentance into words

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. vector<string> explode( const string &delimiter, const string &explodeme);
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. string str = "I have a lovely bunch of cocoa nuts";
  12. cout<<str<<endl;
  13. vector<string> v = explode(" ", str);
  14. for(int i=0; i<v.size(); i++)
  15. cout <<i << " ["<< v[i] <<"] " <<endl;
  16. }
  17.  
  18. vector<string> explode( const string &delimiter, const string &str)
  19. {
  20. vector<string> arr;
  21.  
  22. int strleng = str.length();
  23. int delleng = delimiter.length();
  24. if (delleng==0)
  25. return arr;//no change
  26.  
  27. int i=0;
  28. int k=0;
  29. while( i<strleng )
  30. {
  31. int j=0;
  32. while (i+j<strleng && j<delleng && str[i+j]==delimiter[j])
  33. j++;
  34. if (j==delleng)//found delimiter
  35. {
  36. arr.push_back( str.substr(k, i-k) );
  37. i+=delleng;
  38. k=i;
  39. }
  40. else
  41. {
  42. i++;
  43. }
  44. }
  45. arr.push_back( str.substr(k, i-k) );
  46. return arr;
  47. }
source: cpp explode function
Last edited by blacklight332; Mar 3rd, 2009 at 10:50 pm.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
blacklight332 is offline Offline
5 posts
since Nov 2008
Mar 3rd, 2009
1

Re: splitting sentance into words

You've got a lot of problems with your code, but you do get bonus marks for being one of the few people who use code tags on their first post!

>#include<iostream.h>
>#include<string.h>
Those headers are nonstandard and not recommended. Try removing the ".h" from them and adding the line "using namespace std;" below them.

>#include<conio.h>
>#include<dos.h>
Old headers. Get rid of them.

>#include<stdio.h>
This is a C header. You shouldn't need or use it either.

>void main()
void main() is also nonstandard and bad. Use int main() instead.

>clrscr();
Don't clear the screen. It relies on those old headers, and annoys people like me who might actually have important data on the screen before you wiped it.

>char text1[]=" ";
You're only allocating 2 bytes (one for the space, one for the terminating null character) to hold an entire word. Nope, that's not a good idea.

Instead of using character arrays, consider using a C++ string. You don't need to worry about character allocation, and it makes your job a heck of a lot easier. If you're confident enough, you could even use string's built in searching functions to do the tokenizing instead of implementing your own.

>getch();
This relies on the nonstandard conio.h header. Try using getchar(); instead.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 3rd, 2009
0

Re: splitting sentance into words

go carefully again and again with your algorithm .

If you unable to find it then just try a dry-run.
Entering it the input "hello world"

Think "hello world " and the "hello world" is different
I think you got the point.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 3rd, 2009
0

Re: splitting sentance into words

Quote ...
>char text1[]=" ";
You're only allocating 2 bytes (one for the space, one for the terminating null character) to hold an entire word. Nope, that's not a good idea.
I wonder how this program works. However this will vary due to the memory model that he/she is using.

These types of codes will open your program to bufferoverflow attacks.

Use malloc to allocate memory.
for more information
http://www.cplusplus.com/reference/c...ib/malloc.html
Last edited by NicAx64; Mar 3rd, 2009 at 11:08 pm.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 4th, 2009
0

Re: splitting sentance into words

Click to Expand / Collapse  Quote originally posted by NicAx64 ...
Use malloc to allocate memory.
No, no, no! Use new to allocate memory in C++!!!
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Mar 4th, 2009
0

Re: splitting sentance into words

Or better yet: use std::strings and don't worry about dynamicly allocating memory at all.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Mar 4th, 2009
0

Re: splitting sentance into words

Click to Expand / Collapse  Quote originally posted by niek_e ...
Or better yet: use std::strings and don't worry about dynamicly allocating memory at all.
agree to this !
use the string wrapper class is agreed ! Using the STL as max is somewhat encouraged.

However there are algorithms and every data structures inside the STL . But don't we write our own Stacks and link lists in our school ?

you don't need this while loop at all if you are using the std::string. It comming with powerful functions that already implemented what you are trying to hand-code.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 4th, 2009
0

Re: splitting sentance into words

If you have to implement your own algorithm to do this, try taking a look at a snippet I made some time ago. Link

But otherwise, this is an easy problem, you could even try std::stringstream . You practically don't have to do anything
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main() {
  7. string sentence_s;
  8. stringstream sentence_ss;
  9.  
  10. cout << "Enter a sentence: ";
  11. getline(cin, sentence_s);
  12. sentence_ss << sentence_s;
  13.  
  14. string word;
  15.  
  16. while ( sentence_ss >> word )
  17. cout << '\n' << word;
  18.  
  19. cin.ignore();
  20. }
If you can't do something like this, try using strtok, it will help you split up the word using a delimiter.

Hope this helps.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Mar 5th, 2009
0

Re: splitting sentance into words

If you can't do something like this, try using strtok, it will help you split up the word using a delimiter.

Hope this helps.
Yes a tokenizer is highly recommended.
You will end up using it in half you projects.
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Using gcc to compile C and C++ files together
Next Thread in C++ Forum Timeline: Error in my program: always the same output ...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC