Strings

closed account (DzyTC542)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* 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;
  		else if (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).
Last edited on
slength=sLength;
sLength is initializated at that time, so it is very likely to be a very large number. So it thinks that first line is longest.
closed account (DzyTC542)
ok. Here is what I have now, but it still prints the first string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* 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.
closed account (DzyTC542)
Ok. I tried that but now it just prints the last string. Here's what I have now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* 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;
  		else if (maxlength>slength)
  		max=max;
  		
	}
		printf ("%s", max);
	}
Do not update maxlength too when you found new max string.
Topic archived. No new replies allowed.