I need help on another program. I need to read a list of strings until the word "end" appears and then print the longest string/word. Here's what I've got so far, any help is appreciated.
/* this program reads a list of strings until the word end appears and returns the longest string that was included in the input list.*/
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
int main()
{
string string, max,s2;
int sLength,slength;
printf ("Signal end of output with the word end\n");
printf ("Enter next string");
string= GetLine();
max=string;
s2=string;
slength=sLength;
while (!(StringEqual(string, "end")))
{
printf("\nEnter your next string: ");
string = GetLine();
sLength = StringLength(string);
if (slength>sLength)
max=s2;
elseif (sLength>slength)
max=string;
printf (" The longest string is: %s", max);
}
getchar();
}
My output is always the first string, regardless of everything else. For example, with the inputs 'the' and 'longest', my output is 'the'. Do you know what is the problem?
First thing I notice from the start is you never initialize the value of sLength before you assign it to slength. It might help to have less confusing/similar variable names.
So now both "slength" and "sLength" have junk values, and then you assign a proper value to sLength, but slength still has a junk value when you compare them at if (slength>sLength).
This is assuming all the non-standard #includes you have work as intended.
Also, you should avoid having a variable name be exactly the same as the type name (string string, Line 10).
/* this program reads a list of strings until the word end appears and returns the longest string that was included in the input list.*/
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
int main()
{
string string, max,s2;
int sLength,slength;
printf ("Signal end of output with the world end\n");
printf ("Enter next string");
string= GetLine();
max=string;
s2=string;
slength=StringLength(s2);
while (!(StringEqual(string, "end")))
{
sLength = StringLength(string);
if (slength>sLength) max=s2;
if (sLength>slength) max=string;
string=s2
printf("\nEnter your next string: ");
s2 = GetLine();
}
printf ("The longest string is: %s", max);
getchar();
}
For some reason you are juggling three strings instead of 2. You only need to store current string and max string.
You have problem with those string ordering and what is responsible for what. Remove your s2 string, change code accordingly and everything should work.
/* this program reads a list of strings until the word end appears and returns the longest string that was included in the input list.*/
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"
int main()
{
string string, max;
int sLength, maxlength;
printf ("Signal end of output with the world end\n");
printf ("Enter next string");
string= GetLine();
max=string
maxlength= StringLength (max);
while (!(StringEqual(string, "end")))
{
printf("\nEnter your next string: ");
string = GetLine();
sLength = StringLength(string);
if (sLength>maxlength)
max=string;
elseif (maxlength>slength)
max=max;
}
printf ("%s", max);
}