0

want to be able to count the words in an array or a string :S

but i am not sure how to do it

could i get some help please

what i want to do is be able to tell how many words have been entered. so i will need to store the entry should i use a string? or a char array for it?

and what i want the output to say is
1 letter lengh:
2 letters lengh:

could you just give me some ideas how should i do it? (i am only the beginner xD)

thank you ^_^

Edited by mariusmssj: n/a

3
Contributors
11
Replies
12
Views
8 Years
Discussion Span
Last Post by firstPerson
0

Use strings and sstream.

Get the input as strings. For example :

string input = "";
cout << "<enter text > :\t";
getline(cin,input);

Then use sstream to extract words by words, using the space between
them as delimiters.

If you don't know about the sstream, read up on them because they
are handy.

0

but will that then allow my to do my statistics???

because what i thought would be better is to have it like.

1 letter words: "this many"
2 letter words: "this many"
3 letter words: "this many"
and so on :S

0

jaja i actualy did that the other day.
what you need to do is a for bucle that compares the string or const char* looking for chars.
I belive if(!ispace) should do the trick.

0

jaja i actualy did that the other day.
what you need to do is a for bucle that compares the string or const char* looking for chars.
I belive if(!ispace) should do the trick.

how would i then count the length for words?

0

how would i then count the length for words?

you use string_name.length()
try this

[B]for[/B](integrer = 0; integrer < str.length();integrer++)
0

i still am lost how will i count the length of individual words =( sorry if i am annoying i am still trying to learn

#include <iostream>
#include <string>

int main()
{

	std::string test = "hello there sir";

	for(int count = 0; count < test.length();count++)
	{
	

	}
	
}

Edited by mariusmssj: n/a

0

No :

string str = "12345";
cout << str.length() << endl;

so it just counts the whole amount of elements stored in that string including the space :S

0

so it just counts the whole amount of elements stored in that string including the space :S

Yes.

Unless I am misreading your post, you can just use the advise
in my first pot.

do something like this :

//inside main somewhere

stringstream ss;  //the sstream I was talking about

string sentence = "";

getline(cin, sentence); //get input

string aWord = ""; // will contain each individual word

ss << sentence ; //put our while sentence inside the stream object
 
while (ss >> aWord){ //extract word by word
    cout << aWord <<"  has " << aWord.size() <<" letters\n";
}

You need to include<sstream> to use stringstream.

Edited by firstPerson: n/a

0

[

string sentence = "";
string aWord = "";

no need to empty the string... the are already empty.
And by the way isn't 3 the prime number of 123456789??

Edited by gastonci: n/a

0

[
no need to empty the string... the are already empty.
And by the way isn't 3 the prime number of 123456789??

Nevertheless its always good to manually initialize a variable( it can't hurt). And no the answer is not 3.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.