Word count help.

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

Join Date: Mar 2006
Posts: 7
Reputation: kjones2k1 is an unknown quantity at this point 
Solved Threads: 0
kjones2k1 kjones2k1 is offline Offline
Newbie Poster

Word count help.

 
0
  #1
Apr 7th, 2006
I'm in an entry level c++ class. My professor wants us to write a function that counts the number of words in a string. The stipulation to this is, A WORD IS DEFINED TO BE ANT CONTIGUOUS GROUP OF ASCII CHARS THAT START WITH THE LETTERS A THROUGH Z AND MUST BE UPPER CASE. For example, the sentence: "the Dog At5674 654 Mypaper", there would be 3 words considered in this sentence. The 3 words would be Dog, At5674, and Mypaper. Right now I'm really stumped, My c++ code isn't working I can't figure what I am missing or doing wrong to pring the result out.

Here is my function:
  1. void wordcount()
  2. {
  3. string text = "the Dog At5674 654 Mypaper";
  4. const int x=3;;
  5. int index[x];
  6. int count=0,i,j=0,k;
  7.  
  8.  
  9. for(i=0; i<text.length(); i++)
  10. {
  11. if(isupper(text[i]))
  12. {
  13. count++; //counts number of times a upper case letter is found.
  14. index[i]=i; //stores the position of that capitol letter.
  15. }
  16. }
  17.  
  18. while(sizeof(index))
  19. {
  20. for(k=index[j]; !isspace(text[k]) || text[k] != '\n'; k++)
  21. {
  22. cout<<text[k];
  23. }
  24. j++;
  25.  
  26. }
  27. }

I've found other codes for word count, but you have to keep in mind that this is entry level c++ and we have not touched on vectors, class, istreams, etc.

Thank for the help.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 164
Reputation: orko is an unknown quantity at this point 
Solved Threads: 10
orko orko is offline Offline
Junior Poster

Re: Word count help.

 
0
  #2
Apr 7th, 2006
ok
string text = "the Dog At5674 654 Mypaper";
instead write
char text[max_lim]="the Dog At5674 654 Mypaper";
don't forget to declare the value of the constant max_lim
then instead of the for loop u used, try
int count=1 //assuming u r starting with first word
for(i=1; text[i]!='\0' ;i++) { //finish reading at the end of the string
if(text[i]= ' '} {count++ } // Look for any space bar.
} //one problem is if there is more than one //space bar it will assume thr r more than one word..... u can solve it by //checking the next char if it z string, then the it will simply add one with i
A Perfect World
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: kjones2k1 is an unknown quantity at this point 
Solved Threads: 0
kjones2k1 kjones2k1 is offline Offline
Newbie Poster

Re: Word count help.

 
0
  #3
Apr 8th, 2006
  1. void wordcount()
  2. {
  3. string text = "the Dog At5674 654 Mypaper";
  4. int count=0,i,j,k;
  5.  
  6.  
  7. for(i=0; i<text.length(); i++)
  8. {
  9. if(isupper(text[i]))
  10. {
  11. count++; //counts number of times a upper case letter is found.
  12. }
  13. }
  14.  
  15. cout<<"Total word count: "<<count<<endl<<endl
  16. <<"Words are: "<<endl;
  17.  
  18.  
  19. for(k=0; k<text.length(); k++)
  20. {
  21. if(isupper(text[k]))
  22. {
  23. for(j=k; !isspace(text[j]) && text[j]!='\0'; j++)
  24. cout<<text.substr(j,1); //counts number of times a upper case letter is found.
  25. cout<<endl;
  26. }
  27.  
  28. }
  29. cout<<endl;
  30. return;
  31. }

The code works, but I tried making it where the user inputs the sentence:
  1. string text;
  2.  
  3. cout << "Enter a sentence: " << endl;
  4. getline(cin, text);
This doesn't work for me. Could someone help me out to get this to work? Should I be using a dynamic character pointer?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Word count help.

 
0
  #4
Apr 8th, 2006
getline(cin, text,'\n');
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: kjones2k1 is an unknown quantity at this point 
Solved Threads: 0
kjones2k1 kjones2k1 is offline Offline
Newbie Poster

Re: Word count help.

 
0
  #5
Apr 8th, 2006
Well after talking to a friend in my class, our professor wants us to cin a sentence into an dynamic array. So, if the sentence is longer than the allocated memory, it can just grabs some more. All I want to do at this point is just cin the user inputed sentence, then just cout it back. For example I cin "Daniweb is an excellent place to get help." and the exact sentence is couted back out. Once I get that complete, I can just apply my logic to extract the words from the sentence. Here is what I have so far:

  1. void wordcount()
  2. {
  3.  
  4. int count=0,i,j,k;
  5. char temp[100];
  6. char* b[1000];
  7. int n = 0;
  8.  
  9. cout<<Enter a sentence: "<<endl;
  10. while (cin >> temp)
  11. {
  12. int len = strlen(temp) + 1;
  13. char* newSpace = new char[len];
  14. strcpy(newSpace, temp);
  15. b[n] = newSpace;
  16. n++;
  17. }
  18.  
  19. for(i=0; i<sizeof(b); i++)
  20. cout<<*(b+i);
  21.  
  22. return;
  23. }
  24.  

Obviously I'm doing something wrong, because the code compiles, but when I'm done entering my sentence and I hit return, the cursor just goes to another line.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Word count help.

 
0
  #6
Apr 8th, 2006
  1. char crap[255];
  2.  
  3. std::cout << "Enter a sentence: " << std::endl;
  4. std::cin.getline(crap,255);
  5. std::cout<<crap;

Yes/no ?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 164
Reputation: orko is an unknown quantity at this point 
Solved Threads: 10
orko orko is offline Offline
Junior Poster

Re: Word count help.

 
0
  #7
Apr 9th, 2006
yah.... bcoz ur cursor is waiting for the 100 char as ur full string watch: temp[100] ....... while inputting the characters, break the loop while '\n' will be inserted.... thus u can avoid ur enter button problem..... good luck!
A Perfect World
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 7
Reputation: kjones2k1 is an unknown quantity at this point 
Solved Threads: 0
kjones2k1 kjones2k1 is offline Offline
Newbie Poster

Re: Word count help.

 
0
  #8
Apr 9th, 2006
Originally Posted by orko
yah.... bcoz ur cursor is waiting for the 100 char as ur full string watch: temp[100] ....... while inputting the characters, break the loop while '\n' will be inserted.... thus u can avoid ur enter button problem..... good luck!


How would I insert the '\n' break?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 164
Reputation: orko is an unknown quantity at this point 
Solved Threads: 10
orko orko is offline Offline
Junior Poster

Re: Word count help.

 
0
  #9
Apr 9th, 2006
is thr any requirement how u will understand the input is done?..... assume inputting "end".... or such thing..... if thrz any, just "break" the loop.... (u r in infinity loop bcoz u said while(cin>temp)..... so as long as thr will be input, the loop will alive.... even if u press only "enter" datz another input..... read the assignment requirements carefully.


u can use getline()
A Perfect World
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC