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
Search the whole string for spaces and separate each word into its own char array. Then apply the same method.
closed account (3qX21hU5)
Why would you put them in thier own char array's? I don't get this, if you are using C++ use std::string not c-strings or char arrays.

The better suggestion would be to use a array or even better vector<string> to hold all the words.

You could use string.find_first_of() to find the first space in the sentence, and use string.substr() to copy that word into the vector.
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
Last edited on
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 unsigned short 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.
Last edited on
Topic archived. No new replies allowed.