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 ^_^

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.

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

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.

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?

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++)

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++)
	{
	

	}
	
}

No :

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

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

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.

[

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??

[
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.