I think you need an object of type std::vector<size_t> or std::vector<std::string::size_t> (it depends on approach you will use to resolve the task) that to keep sizes of words.
The simplest approach is to use std::istringstream.
It depends on how you want to do this.
You can make a C string for a word char* word ("hello");
to find out how many letters there are: int letters = sizeof(word) / sizeof (char *);
EDIT:
Sorry, the above would always return 1, because it counts how many words there are.
What I meant is do an array of chars, like: char word[] = ("hello");
And to find out how many chars there are: int letters = sizeof(word) / sizeof (char);
I advice you to write two functions. The first one can be named something as SkipSpaces. The second one can be named ExtractWord. So you will skip spaces and extract words in a loop.
For example
1 2 3 4 5 6
inlineconstchar * SkipSpaces( constchar *s )
{
while ( std::isspace( *s ) ) ++s;
return ( s );
}
In fact this function does the same as standard algorithm std::find_if.
int main()
{
int i, presledek;
char naslednji;
string bes;
presledek=1;
cout << "Podaj poljubno besedilo"<<endl;
getline(cin, bes);
// checks each character in the string
for (i=0; i<int(bes.length()); i++)
{
naslednji = bes.at(i); // gets a character
if (isspace(bes[i]))
presledek++;
}
cout << "Besedilo ima " << presledek<< " besed.";
cin.ignore();
return 0;
}
this code count my word..
what else can i add to the length of words?
example "this is word"
__--------> 4 2 4
on line 13 int( ) is unnecessary length will already return an int. Also on line 15 you don't need to use the at just do bes[ i ] and that will be the character. A string is just an array of characters.
Casting to int will mute the signed/unsigned warning. But using size_t for the index would be better (string::at() takes a size_t, after all)
1 2 3 4 5 6 7 8
// as i is only used in for loop, better to declare it here than at
// the start of the function, like you have to do in C
for (size_t i=0; i<bes.length(); i++)
{
naslednji = bes.at(i); // gets a character
if (isspace(bes[i]))
presledek++;
}
(C++11 allows to use auto for the type, so you could use that to avoid making the choice of type yourself.)