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.)
The original problem statement does not require the words to be displayed, so there is no need to store them. All that is needed are the lengths.
@cosa nostra
I am still unclear on what language feature you would like to/are allowed to use.
But we need you to post your own attempt!
If you think about how you would do it yourself, by eye, the it's:
1 - find the start of the first word
2 - count the letters in it
3 - write that down
4 - find the start of the next word
5 - count the letters in it
6 - write that down
7 - ...
until you've run out of letters.
EDIT If you just want the counts, then it's easier. As you walk the string, if it's a letter, add one to the count of the current word. When it's not a letter, remember the count -- of the last word -- and reset the counter. Easy enough to do with a for-loop and a array of ints (or vector). So you need to focus on isalpha rather than isspace.
(If you want to handle hypenated and contracted words, you'll need to tweak the logic a little bit).
Andy's right, there's no need to separate the string.
You can use a for loop, like
for ( unsigned short letter = 0; letter < word.length(); letter++ )
to process every letter in the word.
Create an unsignedshort variable (int if you prefer) and everytime the loop runs add one to it. If the loop encounters a space, output the variable and a '-', then reset it to 0. After the loop is finished cout that variable once more without '-'.
This won't have the downside of Andy's edit.
Also this will output "0-" if there are a few spaces in a row. You might want to think about how to tweak this... It's not difficult, but I won't tell you how just yet.