Here is my finished program. Please test it out and let me know how it works Hopefully others learning c++ can learn something from my program.. if you have any questions, please ask
The Breakdown:
The longest_word( ) algorithm works like this:
1. Initialize Everything
2. Make a copy of the user input
3. Tokenize the user input (and it's copy) and keep an array of pointers that point to the first letter of each word in the user's input
4. Now that I can readily identify and access each individual word - Compare each word length against every other word (using nested 'for' loops)
5. Return the character count of the longest word
*will only work on compilers that support the non-standard <conio.h> library I like clrscr( ) and getch( ), what can i say
Code:
#include<iostream> //Provides cin, cout, & NULL
#include<cstring> //Provides strcpy(), strtok(), & strlen()
#include<conio.h> //Provides clrscr() & getch()
using namespace std;
//Function Prototypes
int longest_word(char*);
int main( )
{
char user_input[81];
do{
//Input array initialization
for(int c=0; c<80; c++)
user_input[c]=NULL;
clrscr();
cout << "\n\tEnter a sentence: ";
cin.getline(user_input, 80);
cout << "\n\n\tThe longest word contains " << longest_word(user_input)
<< " characters. ";
cout << "\n\n\tWould you like to try again? (Y/N) ";
}while(toupper(getch())=='Y');
clrscr();
return 0;
}
/////FUNCTION DEFINITIONS//////
int longest_word(char* user_input)
{
char temp[81]; //create a temporary array
char* words1[20]; //create an array of pointers (pointers to tokenized words)
char* words2[20]; //create an array of pointers to words
int word_length = 0; //this hold the number of characters in the longest word
int word_count = 0; //will hold number of words in user inputted char array
int longest_word= 0; //will contain the character count of the longest word
//Array Initializations
for(int c=0; c<80; c++)
temp[c]=NULL;
for(int c=0; c<20; c++)
{
words1[c]=NULL;
words2[c]=NULL;
}
//Make a copy of user_input[]
strcpy(temp, user_input);
//Begin array tokenization routines
//create an array of pointers to words
//
//These tokenization routines are based
//on this web tutorial:
//http://www.cplusplus.com/ref/cstring/strtok.html
//
//Tokenize temp array
word_count=1;
words1[0] = strtok(temp, " ");
while(words1[(word_count-1)]!=NULL) //examine the previously entered element, and terminate the loop
{ //if previous element == NULL
words1[word_count] = strtok(NULL, " ,."); //this might look kinda complex, but strtok( ) is basically
word_count++; //just returning pointers to each word from the temp[] array.
};
//Tokenize user_input array
word_count=1;
words2[0] = strtok(user_input, " ");
while(words2[(word_count-1)]!=NULL)
{
words2[word_count] = strtok(NULL, " ,."); //populate words2[] with pointers to the words in user_input[]
word_count++;
};
//The purpose of this next block of code, is to compare each
//word against every other word
//
//The outter loop will be used to step through the words2[] word pointer array
//Each iteration of the outter loop will increment to the next word
//of words2 (user_input) using the 'p' loop counter.
for(int p=0; p<(word_count-1); p++)
{
//The inner loop will continuously cycle through words1[] pointer array comparing
//each word to that of words2[] (user_input array)
for(int q=0; q<(word_count-1); q++)
{
//if word1 is greater than word2
if(strlen(words1[q]) > strlen(words2[p]))
word_length = strlen(words1[q]);
//if word1 is less than or equal to word2
else
word_length = strlen(words2[p]);
}//end INNER for
if(word_length>longest_word)
longest_word = word_length;
}//end OUTTER for
return longest_word;
}//end FUNCTION