944,033 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5905
  • C++ RSS
Apr 7th, 2006
0

Word count help.

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjones2k1 is offline Offline
7 posts
since Mar 2006
Apr 7th, 2006
0

Re: Word count help.

ok
Quote ...
string text = "the Dog At5674 654 Mypaper";
instead write
Quote ...
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
Quote ...
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
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Apr 8th, 2006
0

Re: Word count help.

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjones2k1 is offline Offline
7 posts
since Mar 2006
Apr 8th, 2006
0

Re: Word count help.

getline(cin, text,'\n');
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 8th, 2006
0

Re: Word count help.

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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjones2k1 is offline Offline
7 posts
since Mar 2006
Apr 8th, 2006
0

Re: Word count help.

C++ Syntax (Toggle Plain Text)
  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 ?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 9th, 2006
0

Re: Word count help.

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!
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Apr 9th, 2006
0

Re: Word count help.

Quote 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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjones2k1 is offline Offline
7 posts
since Mar 2006
Apr 9th, 2006
0

Re: Word count help.

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()
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006

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: need ur help in c++ coding plz help
Next Thread in C++ Forum Timeline: Dll from C++ to C#





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


Follow us on Twitter


© 2011 DaniWeb® LLC