word length

TASK.
How many words is in a given text.Print the word length.

example: THIS IS A WORD

display - text has 4 words. LENGTH OF WORD ARE 4-2-1-4

please code ..tnx
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.
Last edited on
@cosa nostra

Have you done vectors, stringstream yet? Your earlier posts suggest you are probably at an early stage of your C++ education...

So are you expected to code a solution using C-style arrays, operator[], etc. ? Or can you use the C++ standard library classes, as vlad' suggested?

Note: we are not supposed to provide the code for you: just help you to correct your code!

Andy
Last edited on
yes i am beginer. no ve havent done with vectors
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);
Last edited on
1
2
3
4
5
6
string bes;
cout << "podaj besedilo"<<endl;

getline(cin,bes);
cout << bes << " - "<<bes.length();
cout << " znakov"<<endl;


how to divorce spaces
I had made a mistake above. Now that's fixed.

cosa nostra wrote:
how to divorce spaces
Divorcing spaces? What do you mean?
how to separate spaces

exampla THIS IS WORD.
4 2 4 no spaces
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
inline const char * SkipSpaces( const char *s )
{
    while ( std::isspace( *s ) ) ++s;

    return ( s );
}


In fact this function does the same as standard algorithm std::find_if.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.
on line 13 int( ) is unnecessary length will already return an int.

Nope!

The signature of string::length() is:

size_t string::length() const;

Where size_t is an unsigned integral type: usually either a 32-bit unsigned int or 64-bit unsigned int.

See http://www.cplusplus.com/reference/string/string/length/

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

Andy

ah I figured size_t just meant unsigned long( int ) didn't realize it could be a long long also. Guess I'll have to read up more on size_t thanks.
cout<<bes.length() this is for the full text.
example ." please hepl my" ----> 15

how to display the length of each word
example "please hepl my" ----> 6 - 4 - 2
Registered users can post here. Sign in or register to post.