Hello,
I teach C++.
Here is a quickly coded up solution for you. It should be quite optimised, by avoiding unnecessary /redundant IF conditions/checks(and so on).
If you like it , I can do the other one for you, once the bid is accepted.
#include <stdio.h>
int GetMaxNumWordsInSentence (char *str)
{
int nTotWords=0; // total words found in loongest sentance
int nWordCount=0,i=0;
bool bInMiddleOfWord=false;
char ch=0;
for (i=0; str[i] != '\0' ; i++)
{
ch = str[i];
if(!bInMiddleOfWord)
{
if( (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
{
bInMiddleOfWord = true;
}
}
else
{
if (ch == ' ') // end of word encountered
{
nWordCount++;
bInMiddleOfWord = false;
}
else
if (ch == '.' || ch == '!' || ch == '?') // end of sentance encountered
{
if(++nWordCount > nTotWords) // compare number of words found in last sentance, with num of words in this sentance
nTotWords = nWordCount;
nWordCount =0;
}
}
}// end for
return nTotWords;
}
int main(int argc, char *argv[])
{
char szBuffer[100]= "We will see what the time is. Happy Hour is now?";
int sol = GetMaxNumWordsInSentence(szBuffer);
printf("\n\rLongest sentance contained %d words\n\r", sol);
char szBuffer2[100]= "Fore score and seven years ago. This is a great car!";
int sol2 = GetMaxNumWordsInSentence (szBuffer2);
printf("\n\rLongest sentance contained %d words\n\r", sol2);
printf("\n\rHelllo world!\n\r");
return 0;
}