Long string

closed account (DzyTC542)
Hi everyone. I need more help. I am have to write a program that reads a list of strings until the word "end" appears and returns the longest string from the input list. For some reason, it does not compile. Here's what I have, and as usual, all help is needed and 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
  /* 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 <genlib.h>
#include "stdio.h"
#include "strlib.h" 
#include "simpio.h"
int main() 
{ 
string str, max,x;
    printf ("Please enter list of strings.\nAs a sentinel value use: end.\n"); 
    str=getstring (); 
     max=0;
	 x=str; 
     
     while (getline && str != "end") 
     { 
          
         x= stringequal <str>; 
             max=x;
			 else 
             printf ("Enter next string"); 
             str=GetLine ();
    
     } 
     printf ("The longest string is:%d " ,max); 
}
I think you need to #include <string> as well.
closed account (DzyTC542)
Ok thanks. I did and it still didn't work. Any other tips?
That doesn't look like standard C or C++.
getstring () and stringequal <str> are not something I recognise.

in C++ an outline might look like this, though I didn't do it all for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main() 
{ 
    string str;
    cout << "Please enter list of strings.\nAs a sentinel value use: end.\n"; 

    while (getline(cin, str) && str != "end") 
    { 
        cout << "length = " << str.size() << endl; 
    } 
    
    cout << "done" << endl;
}


while the same thing in C might look like this;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <string.h>

int main() 
{ 
    char str[1024];
    printf("Please enter list of strings.\nAs a sentinel value use: end.\n"); 

    while (gets(str) && strcmp(str, "end")) 
    { 
        printf("length = %d\n", strlen(str)); 
    } 
    
    printf("done\n");
}
Last edited on
closed account (DzyTC542)
ok but then do I write str=GetString () under please enter a list of strings etc?
Could you clarify what you are asking here. I'm not familiar with GetString ()

I omitted the part where the longest string is saved. Do you want to know about C or C++ - or perhaps neither? If you need help with using functions from these headers:
1
2
3
#include <genlib.h>
#include "strlib.h" 
#include "simpio.h"  
then I'm unable to help, I don't recognise them.
closed account (DzyTC542)
When a user enters an integer, I write GetInteger();. Therefore, when the user enters a string, would I write GetString(); or something else? I'm talking about C and why did you omit the part that saves the longest string?
I'm talking about C
Thank you.
and why did you omit the part that saves the longest string?
On this site, we offer as much help as people need. However, that usually doesn't necessarily mean we do all of the work, it's a matter of helping you to help yourself.

when the user enters a string, would I write GetString(); or something else?
I answered that in the previous post, gets(str) would be a way to do it. (there are other ways).

[edit: this function is deprecated, that is I shouldn't really recommend it. fgets() is an alternative, but not quite the same. Sorry if this was misleading, I'm more familiar with C++].

If you want to make a copy of a string in C, allocate a buffer where it will be stored:
 
    char longest[1024];
and copy one string to another like this:
 
    strcpy(longest, str); // copy str to longest 


I previously showed strlen(), that returns the length of the string. And a string can be printed with printf("%s", str) or perhaps puts(str);

http://www.cplusplus.com/reference/cstdio/gets/
http://www.cplusplus.com/reference/cstring/strcpy/
http://www.cplusplus.com/reference/cstring/strlen/
http://www.cplusplus.com/reference/cstring/strcmp/


Last edited on
Topic archived. No new replies allowed.