splitting sentance into words

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 3
Reputation: sunderthomas is an unknown quantity at this point 
Solved Threads: 0
sunderthomas sunderthomas is offline Offline
Newbie Poster

splitting sentance into words

 
0
  #1
Mar 3rd, 2009
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.....

  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: blacklight332 is an unknown quantity at this point 
Solved Threads: 0
blacklight332 blacklight332 is offline Offline
Newbie Poster

Re: splitting sentance into words

 
0
  #2
Mar 3rd, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: splitting sentance into words

 
1
  #3
Mar 3rd, 2009
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 316
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: splitting sentance into words

 
0
  #4
Mar 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 316
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: splitting sentance into words

 
0
  #5
Mar 3rd, 2009
>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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: splitting sentance into words

 
0
  #6
Mar 4th, 2009
Originally Posted by NicAx64 View Post
Use malloc to allocate memory.
No, no, no! Use new to allocate memory in C++!!!
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,958
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 307
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: splitting sentance into words

 
0
  #7
Mar 4th, 2009
Or better yet: use std::strings and don't worry about dynamicly allocating memory at all.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 316
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: splitting sentance into words

 
0
  #8
Mar 4th, 2009
Originally Posted by niek_e View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,482
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: splitting sentance into words

 
0
  #9
Mar 4th, 2009
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: splitting sentance into words

 
0
  #10
Mar 5th, 2009
Originally Posted by William Hemsworth View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC