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, " ,.-");
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..
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
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.