ok i think i did that and I am now able to display the amount of characters the longest word has. Now my problem is displaying that word on the screen. I may be overcomplicating things again, but what I am thinking is to scan each letter into an array during the loop and then after each space rescan over that array if the word is longer. The rest of the array is filled with spaces to avoid random garbage.
Is this even possible to do, or is there a simpler way? Again any help and a point in the right direction will be greatly appreciated. Thank you.
This is my new code:
Code:
#include <stdio.h>
#include <string.h>
int
wordcount (char string[])
{
int words=0, i;
for (i=0; i<strlen(string); ++i)
if (string[i]==' ')
words = words + 1;
return (words);
}
int
charcount (char string2[])
{
int chars=0, i;
for (i=0; i<strlen(string2); ++i)
if (string2[i]!='\0')
chars = chars + 1;
return (chars);
}
int
sentence_count (char string3[])
{
int sentences=0, i;
for (i=0; i<strlen(string3); ++i)
if (string3[i]=='.'&& string3[i+2]>'A' && string3[i+2]<'Z')
sentences = sentences + 1;
return (sentences);
}
int main (void)
{
char s[2000];
int wordnum, i, charnum, sentnum, j, letternum=0, max=0;
FILE *input;
input = fopen("phrase.txt", "r");
fgets (s, sizeof(s), input);
printf ("The sentence is:\n\"%s\"\n\n\n", s);
wordnum = wordcount (s);
++wordnum;
printf ("The sentence contains %d words.\n", wordnum);
charnum = charcount (s);
printf ("The sentence contains %d characters.\n", charnum-1);
sentnum = sentence_count (s);
printf ("There are %d sentences.\n", sentnum+1);
for (i=0; i<strlen(s); ++i)
if (s[i]>='A'&& s[i]<='Z'||
s[i]>='a'&&s[i]<='z')
{
letternum = letternum + 1;
}
else if(letternum>=max)
{
max=letternum;
letternum=0;
}
else
{
letternum = 0;
}
printf ("The longest word has %d letter.\n", max);
return (0);
}