### Find length of every word.

While bored of DBs and Oracle started practicing C++.

Here is the problem: It's requested, they want me to create a char array, fill it with words (doesn't matter if meaningful or not) then find spaces that were used, then find how many words, then how long is EACH word.

This is what I've done so far but only missing that 'how long is each word'.
</code
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>

using namespace std;

int main()
{
char symbarray[255];
char * pch;
int word = 0;
int longestWord = 0;
cout << "Enter whatever you want up to 100 symbols" << endl;
cin.getline(symbarray, 100);

/* This finds me how many empty spaces there are in the string */
for (int x = 0; x < 100; x++)
{
if (symbarray[x] == ' ')
word++;
}
/* Unless started with an empty space (which I find stupid), words are empty spaces + 1 */
cout << "Numbers of words entered is: " << word + 1 << endl;

/* strtok is something I found on the internet which I have a hunch could be used it breaks down the string to words, now I struggle how to 'compare' / measure these words length, I attempted strlen() but obviously it fails */
pch = strtok(symbarray, " ,.-");

while (pch != NULL)
{
printf("%s\n", pch);
pch = strtok(NULL, " ,.-");

if (strlen(pch) < longestWord)
longestWord = strlen(pch);
}
cout << "Longest entered word is: " << longestWord << endl;

return 0;
}
/>code
I was thinking to count chars while I reach empty space, and the highest count to be my longest word but I struggle to put it on the sheet :) (do you find this idea reasonable) ?

I am not looking for a solution it is of no use for me, I am looking for ideas, or I will never learn..
This is what I came up with, however a bit of explanation is needed:

</code

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>

using namespace std;

int main()
{
char symbarray[100];
string s;
int word = 0;
int sum = 0;
int total = 0;
cout << "Enter whatever you want up to 100 symbols" << endl;
cin.getline(symbarray, 100);
/* Unless started with an empty space (which I find stupid), words are empty spaces + 1 */
for (int x = 0; x < 100; x++)
{
if (symbarray[x] == ' ')
word++;
}

cout << "Numbers of words entered is: " << word + 1 << endl;

/* Length of words */
for (int k = 0; k < 100; k++)
{
if (symbarray[k] != ' ' && symbarray[k]) {
s += symbarray[k];

}
else {
cout << s << " " << s.length() << endl;
sum += s.length();

s = "";
}
}

return 0;
}

/>code

Output:

Enter whatever you want up to 100 symbols
we are the future except we are not
Numbers of words entered is: 8
we 2
are 3
the 3
future 6
except 6
we 2
are 3
not 3

What I believe I did is (of course various internet sources helped, started looping through my initial array (symbarray) when I do not encounter empty space I put in words in string s, however I struggle to understand this particular condition:

if (symbarray[k] != ' ' && symbarray[k])

does this mean if you're not blank AND you're ... the initial string, keep assigning values in string s

Now I need some lecturing

if ( symbarray[k] != ' ' && symbarray[k] ) is equivalent to
if ( symbarray[k] != ' ' && symbarray[k] != 0 )

In words, "if the character at position k is not a space, and it is also not the null character"

Note that we may reach the end of a particular word because of either one of these two reasons:
a. the next character is a space
b. we have reached the end of the string; the next character is the terminating null character.
Thanks mate!

Another question, accessing char arrays using loops seems to be the best way in c++, however

for instance if i'd like to copy every word should I use the same approach:

for (int k = 0; k < 100; k++)
{
if (symbarray[k] != ' ' && symbarray[k]) {
s += symbarray[k];

}
else {
strcpy(tmpary, symbarray[k]);

}
}

?

> accessing char arrays using loops seems to be the best way in c++

Using std::string instead of arrays of char is the best way in C++

> i'd like to copy every word should I use the same approach

std::string is a copyable type; we can copy strings in the same way that we copy integers.

Use the standard library; in particular use std::string and std::vector.
That is the right way to start learning C++.

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include #include #include #include int main() { // https://cal-linux.com/tutorials/strings.html std::string line ; std::cout << "enter a line of text\n" ; std::getline( std::cin, line ) ; std::cout << "\nthe line is: '" << line << "'\n" ; std::size_t num_spaces = 0 ; // http://www.stroustrup.com/C++11FAQ.html#for // http://en.cppreference.com/w/cpp/string/byte/isspace for( char c : line ) if( std::isspace(c) ) ++num_spaces ; std::cout << "#spaces: " << num_spaces << '\n' ; // https://cal-linux.com/tutorials/vectors.html std::vector all_words ; // split the line into white space delimited words // https://latedev.wordpress.com/2011/11/16/c-stringstreams/ std::istringstream stm(line) ; std::string word ; while( stm >> word ) all_words.push_back(word) ; // add each word to the vector std::cout << "#words: " << all_words.size() << "\n\n" ; // http://www.stroustrup.com/C++11FAQ.html#for for( const std::string& word : all_words ) // for each word in all_words std::cout << word << ' ' << word.size() << '\n' ; }``````

http://coliru.stacked-crooked.com/a/21da6da2d0a82531
Topic archived. No new replies allowed.