By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,292 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

I cant find my longest word , please help

P: 3
can someone point out my error?

cus this is printing out garbage and not the longest word...

Program ( I underlined the loop Im trying to make to read and find the longest word) everything else works perfectly:

#include <stdio.h>
#include <string.h>



int main()
{

char poem_w[300],poem_s[2000],e[100],longestword1[50];
char poem_w2[300],poem_s2[2000],e2[100],longestword2[50];
char poem_w3[300],poem_s3[2000],e3[100],longestword3[50];
int slength,swlength;
int sentence=0,sentence2=0,sentence3=0;
int word=0,word2=0,word3=0,word4=0;
int i=0,j=0,k=0,l=0,m=0;
int character=0;


FILE *file1,*file2,*file3;
{
file1 = fopen("french_orig.txt","r");

while(fscanf(file1,"%s",poem_w)!=EOF)
{
i ++;
strcpy(poem_s,poem_w);
}
word = i;

printf("\n~*Les Chats: Original*~\n\n");

file1 = fopen("french_orig.txt","r");
fgets (poem_s, sizeof(poem_s), file1);

printf ("%s", poem_s);

slength = strlen(poem_s);

for (i=0; i<=slength; i++)
if (poem_s[i] == '.')
sentence ++;

for (i=0;i<slength;i++)
{
if (poem_s[i]!= '.' && poem_s[i]!=';' && poem_s[i]!=',' && poem_s[i]!=' ');
}
{
printf("\n\n There are: \n %d words.\n %d sentences.\n %s is the longest word.",word,sentence, longestword1);
}

while(fscanf(file1,"%s",poem_w)!=EOF)
{

i++;
swlength = strlen(poem_w);
strcpy(poem_s,poem_w);
}

character = i;;
slength = strlen(poem_s);
for (i = 0; i <= slength; i++);
{
printf("\n %d characters.\n\n",character);
}

fclose(file1);
Nov 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: 15
can someone point out my error?

cus this is printing out garbage and not the longest word...

Program ( I underlined the loop Im trying to make to read and find the longest word) everything else works perfectly:

#include <stdio.h>
#include <string.h>



int main()
{

char poem_w[300],poem_s[2000],e[100],longestword1[50];
char poem_w2[300],poem_s2[2000],e2[100],longestword2[50];
char poem_w3[300],poem_s3[2000],e3[100],longestword3[50];
int slength,swlength;
int sentence=0,sentence2=0,sentence3=0;
int word=0,word2=0,word3=0,word4=0;
int i=0,j=0,k=0,l=0,m=0;
int character=0;


FILE *file1,*file2,*file3;
{
file1 = fopen("french_orig.txt","r");

while(fscanf(file1,"%s",poem_w)!=EOF)
{
i ++;
strcpy(poem_s,poem_w);
}
word = i;

printf("\n~*Les Chats: Original*~\n\n");

file1 = fopen("french_orig.txt","r");
fgets (poem_s, sizeof(poem_s), file1);

printf ("%s", poem_s);

slength = strlen(poem_s);

for (i=0; i<=slength; i++)
if (poem_s[i] == '.')
sentence ++;

for (i=0;i<slength;i++)
{
if (poem_s[i]!= '.' && poem_s[i]!=';' && poem_s[i]!=',' && poem_s[i]!=' ');
}
{
printf("\n\n There are: \n %d words.\n %d sentences.\n %s is the longest word.",word,sentence, longestword1);
}

while(fscanf(file1,"%s",poem_w)!=EOF)
{

i++;
swlength = strlen(poem_w);
strcpy(poem_s,poem_w);
}

character = i;;
slength = strlen(poem_s);
for (i = 0; i <= slength; i++);
{
printf("\n %d characters.\n\n",character);
}

fclose(file1);
Hi,

Started reading your program and failed to understand why you are doing this..

while(fscanf(file1,"%s",poem_w)!=EOF)
{
i ++;
strcpy(poem_s,poem_w);
}
word = i;

I dont want to go any further with out knowing what exactly you want to do...with the above snippet of code.

Anyways here is a small code i wrote which will find out the longest word and prints it.

Beware: It will give you a single longest word. If you have two longest words of same length, this program will select the first found word and prints it..


#include <stdio.h>
#include <stdlib.h>

void main()
{
FILE *fp;
char wordread[200], wordfound[200], charread;
int count = 0, Maximus = 0, CurrLen;
fp = fopen("c:/longword.txt","r");
strcpy(wordread,"");
while(fp)
{
charread = fgetc(fp);
wordread[count] = charread;
count++;
if (charread == EOF)
break;

if (charread == '.' || charread == '\n' || charread == ' ')
{
if( count-1 > Maximus )
{
Maximus = count-1;
strcpy(wordfound,wordread);
}
strcpy(wordread,"");
count=0;
}
}
printf("The largest word is of length: %d\n", Maximus);

printf("The largest word is: ");
for(int i = 0; i < Maximus; i++)
printf("%c",wordfound[i]);
printf("\n");
fclose(fp);
}


This might help you ...

Attention!! The above program is simple but not nice... ;)

Regards,
ShaggY@FtF
Nov 30 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.