473,800 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I cant find my longest word , please help

3 New Member
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,swlengt h;
int sentence=0,sent ence2=0,sentenc e3=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_o rig.txt","r");

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

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

file1 = fopen("french_o rig.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,sen tence, longestword1);
}

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

i++;
swlength = strlen(poem_w);
strcpy(poem_s,p oem_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
1 4004
svsandeep
15 New Member
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,swlengt h;
int sentence=0,sent ence2=0,sentenc e3=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_o rig.txt","r");

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

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

file1 = fopen("french_o rig.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,sen tence, longestword1);
}

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

i++;
swlength = strlen(poem_w);
strcpy(poem_s,p oem_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(fi le1,"%s",poem_w )!=EOF)
{
i ++;
strcpy(poem_s,p oem_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(wordfoun d,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",wor dfound[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

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

Similar topics

14
2121
by: Mudge | last post by:
This thread is hereby dubbed PHP Purpose, or PHPP. It has the following purposes: 1. To bring together PHPers and programmers around the world to become organized. 2. To dicuss purposes of programmers as relates to motives for programming. 3. To dicuss purposes of PHP itself and related technologies.
3
33870
by: hokiegal99 | last post by:
How do I say: x = string.find(files, 'this', 'that', 'the-other') currently I have to write it like this to make it work: x = string.find(files, 'this') y = string.find(files, 'that') z = string.find(files, 'the-other')
4
2472
by: gregsands | last post by:
Hi I have read all (ok most) of the posts relating to "Call to undefined function mysql_connect()", read the manual on PHP.net and done eveything thats ive been asked to do but cant get PHP to talk to MySQL, PHP is working fine but I dont get any mysql info when running <? phpinfo() ?> and get the "Call to undefined function mysql_connect()" when trying to connect to mysql through php in browser, please help. 1. I have added C:\php;...
1
1978
by: Arjen van der Hulst | last post by:
Hi all, I am trying to implement some Word-automation using the range.find-object (using VB.net and Word 2000, early binding). The sample underneath worked well, but after installing an 'after SP3 patch' for Word ('upgrading' it to build 9.0.8216), it stopped working and now generates the following error when I try to set the style-part of the range.find object.
3
1933
H0kage
by: H0kage | last post by:
Hello everyone got an assignment for college where i cant find the way to make JDeveloper to understand where the words are separated.This What The Assignment Asks.Any Tip or example to help me finish it would be greatly apreaciated.Thank you in advance. Following is an example of screen results that might appear, depending on the data the user inputs. This program asks the user for three words.
1
1920
by: niraj | last post by:
I am getting the following error /usr/bin/ld: cant find -lutl Please advice which module i need to install .I am unable to find it . TIA Niraj
4
3057
by: Thomas Mlynarczyk | last post by:
Hello, I have two arrays like this: $aSearch = array( 'A', 'B', 'C.D', 'E', 'F' ); $aSubject = array( 'A' =0, 'A.B' =1, 'X' =2, 'C.D.E' =3, 'A.B.C' => 4 ); Now I want to search $aSubject for the longest key that consists of the first x elements of $aSearch concatenated by "." (where x = 1...count(
9
3008
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that directory. Assume that all contents of all the files may be held in memory at the same time. Do not use unsafe code blocks and/ or pointers.
3
2818
by: sedaw | last post by:
hello ! i need to find the longest sub series by recursion , without use loops . for example : if the series: arr = 45 1 21 3 33 6 53 9 18 the sub series are:
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.