| | |
Word count help.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
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:
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.
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)
void wordcount() { string text = "the Dog At5674 654 Mypaper"; const int x=3;; int index[x]; int count=0,i,j=0,k; for(i=0; i<text.length(); i++) { if(isupper(text[i])) { count++; //counts number of times a upper case letter is found. index[i]=i; //stores the position of that capitol letter. } } while(sizeof(index)) { for(k=index[j]; !isspace(text[k]) || text[k] != '\n'; k++) { cout<<text[k]; } j++; } }
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.
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
ok
instead write don't forget to declare the value of the constant max_lim
then instead of the for loop u used, try
•
•
•
•
string text = "the Dog At5674 654 Mypaper";
•
•
•
•
char text[max_lim]="the Dog At5674 654 Mypaper";
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
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
void wordcount() { string text = "the Dog At5674 654 Mypaper"; int count=0,i,j,k; for(i=0; i<text.length(); i++) { if(isupper(text[i])) { count++; //counts number of times a upper case letter is found. } } cout<<"Total word count: "<<count<<endl<<endl <<"Words are: "<<endl; for(k=0; k<text.length(); k++) { if(isupper(text[k])) { for(j=k; !isspace(text[j]) && text[j]!='\0'; j++) cout<<text.substr(j,1); //counts number of times a upper case letter is found. cout<<endl; } } cout<<endl; return; }
The code works, but I tried making it where the user inputs the sentence:
C++ Syntax (Toggle Plain Text)
string text; cout << "Enter a sentence: " << endl; getline(cin, text);
•
•
Join Date: Mar 2006
Posts: 7
Reputation:
Solved Threads: 0
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:
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.
C++ Syntax (Toggle Plain Text)
void wordcount() { int count=0,i,j,k; char temp[100]; char* b[1000]; int n = 0; cout<<Enter a sentence: "<<endl; while (cin >> temp) { int len = strlen(temp) + 1; char* newSpace = new char[len]; strcpy(newSpace, temp); b[n] = newSpace; n++; } for(i=0; i<sizeof(b); i++) cout<<*(b+i); return; }
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.
C++ Syntax (Toggle Plain Text)
char crap[255]; std::cout << "Enter a sentence: " << std::endl; std::cin.getline(crap,255); std::cout<<crap;
Yes/no ?
*Voted best profile in the world*
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
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()
u can use getline()
A Perfect World
![]() |
Similar Threads
- word count (C++)
- Can you please help me write this word count program in another way (Python)
- I Need Help Writing A Word Count Program In My Python (Python)
- Need Help, basic word count, string manipulation (VB.NET)
- word count in borland c++ ?? (C++)
- word count (Java)
- I can't implement a word count into my text editor (JAVA) (Java)
Other Threads in the C++ Forum
- Previous Thread: need ur help in c++ coding plz help
- Next Thread: Dll from C++ to C#
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






