Click to See Complete Forum and Search --> : words count


arlenagha
03-06-2003, 04:30 AM
hello everyone.

here is my own first algorithm. it only counts the number of words in a line.
from my testings it works just fine.

let me know what you think and if there is a better way to write it.



#include <iostream>
#include <string>

using namespace std;

int main(){

int wordCount = 0;

string words;
getline(cin, words);
int length = words.length();

for(int index = 0; index <= length; ++index){

while(words[index] != 32){
++index;
}
++wordCount;
while(words[index] == 32){
++index;
}

}
if(words[0] == 32)
--wordCount;

cout << wordCount << endl;

return 0;

}

truls
03-06-2003, 05:31 AM
It's just my talent, but on my first try it just hung.

The problem is that the program can hang in the while loops, since you are increasing index but don't test if index goes beyond length. If you change your while loops, and add (... && index<length) it should work fine.

And the program is fine, but of course there is an easier way to do it:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){
int wordCount = 0;

// getline() doesn't work on my windows machine
// so I just use a sample string. Replace this line
// your getline() version.
string words = "This is a test of this program ";
istringstream str( words );
while( str >> words ) wordCount++;
cout << "Wordcount = " << wordCount << endl;
return 0;
}

arlenagha
03-06-2003, 07:18 AM
window....ah your windows??? what???

I thought this was a linux forum??? ;</

arlenagha
03-06-2003, 07:23 AM
...and index isn't going beyond length and is being tested since index <= length in the for loop. am i correct??

truls
03-06-2003, 08:05 AM
I'm stuck at work, and the IT department are all windows people - thus I'm stuck at a windows machine (and how much I long for the unix tools).

You do check for length, but what happens in:
while(words[index] != 32){
++index;
}

What stops this from just going, and going, and going.... :p
(Apart from space, I know. But what if a line consists of only letters?).

spacedog
03-06-2003, 01:55 PM
I was getting a seg fault. So I fixed a little.

The algorithm is generally right, but I would change and add a few things

first. when you check for a new word using the while(words[i] != 32) you should also check if you encountered the terminating string
while(words[i] != 32 && words[i] != '\0')


Second the second while loop is not necessary, if you want to keep it, make it an if statement.

lastely when checking
if(words[0] == 32)
--wordCount;

you should do a
if(!words[0])

because lets say you hit enter when prompted to enter a sentence, getline will not put anything in words. therefore words[0] is NULL.




#include <iostream>
#include <string>

using namespace std;

int main(){

int wordCount = 0;
int index=0;
string words;
getline(cin, words);
int length = words.length();

do{
while(words[index] != ' ' && words[index] != '\0'){
index++;
}
wordCount++;
index++;
}while(words[index-1] != '\0');

if(!words[0])
wordCount = 0;

cout << wordCount << endl;

return 0;

arlenagha
03-06-2003, 07:17 PM
thanks spacedog, i prefer your improved version, works much better.

arlenagha
03-07-2003, 12:16 AM
well spacedog i was being sarcastic given the fact that there is a serious flaw in your code.

try typing in more than one space between the words and it will count each extra space as a word.

augur did post this in the C Board which is short and a better version of my algorithm.

#include <iostream>
#include <string>
#include <sstream>

int main()
{
int wordCount = 0;
std::string words;
std::getline(std::cin, words);
std::istringstream is(words);
while (is >> words)
wordCount++;
std::cout << wordCount << std::endl;
return 0;
}

spacedog
03-07-2003, 02:31 AM
well usually there is only one space between words in a sentence. Right? Either way it would be trivial to skip white spaces, just change the if() to a while().

Second when you are trying to come up with an algorithm you usually have to do the coding instead of using library functions.. Especially for something as trivial as counting words in a sentence.
For example if you write a bubblesort algorithm you have to come up with the algorithm, not just use a bubblesort() library function that someone else has written.

thanks for the sarcasm... real cool...

truls
03-07-2003, 04:05 AM
Do people read my posts at all?