GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Jul-2009, 04:33
Pefectionist Pefectionist is offline
New Member
 
Join Date: Jul 2009
Posts: 4
Pefectionist is on a distinguished road

How to finish this C Code?


Objective: Input: "This is funny" "end (signals as stop)" Output "The longest string is: funny"

CPP / C++ / C Code:
#include "simpio.h"
#include "genlib.h"
#include <stdio.h>
#include <strlib.h>
#include <string.h>
#define strEqual strcmp

main()
{
      string input;
      string StringLength, resultStr;
      resultStr="";
      printf("Singal the end of your input list with the wod end\n");
      printf("Enter first string?\n");
      input=GetLine();
      
      
      while(!(strEqual (input, "end"))
      {
                     StringLength(input);
                     printf("Enter Next String?\n");
                     input = GetLine();
                     }
                     if StringLength(input)<input
      
                    /*This is the part where I'm lost */

                     printf("The longest string is: %s\n", );
                     system("pause");
                     }
Last edited by LuciWiz : 16-Jul-2009 at 04:52. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 16-Jul-2009, 09:32
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: How to finish this C Code?


What are "StringLength()" , "GetLine()" , "simpio.h" and "genlib.h" ?

If you want folks to help you should stick with standard C functions and header files.
acm.uiuc.edu/webmonkeys/book/c_guide/
If you created "simpio.h" and "genlib.h" then you should include them with your code.

Do not use "system("pause"); " . It is not standard C. It is a traitor and part of the Empire.
Instead use getchar(); to pause.

"string.h" ( the header file you get strcmp() from ) also has a function "strlen()" to use instead of "StringLength()" .
It has many other handy string related functions as well. (see string.h in the above reference)
If you want to find the longest string and have it on hand you will need to be able to store it for a while.
You will need at least two string arrays. One to store the longest and one to store the current input strings.

I would suggest learning to use getchar() to get your input into arrays. Here are some ideas:
CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  char s[5][128] = {{0}};  /* array of 5 char arrays of 128 chars each initialized to zero */
  int c, i, j, maxy, maxx, longest= 0, longestct= 0 ;

  maxy = ((int)sizeof(s) / (int)sizeof(s[0]));
  maxx = (int)sizeof(s[0]);

  printf("Array s is: %d by %d \n", maxy , maxx);
  
  for(i = 0; i < maxy; i++)
  {
    printf("\nEnter string %d: \n", i); 
    for(j = 0; (c = getchar()) != '\n'; j++ )  /* reading until \n will flush all chars out of stdin */
    {
       if(j < maxx - 1 ) /* leave room for a zero string terminator */
       {                 /* It's VERY important to write only within the bounds of the array. */
         s[i][j] = c;
       }
    }
    if(j >= maxx)
    {
      j = maxx;
    }
    s[i][j] = '\0';       /* add zero terminator to mark the end of the string */
    if( (j - 1) > longestct)
    {
      longestct = (j - 1);
      longest = i;
      printf("     (That was the longest so far) \n");
    }
  }

  printf("\n----------------\nAnd the winner at %d characters is %d: \n<%s>",
                                         longestct, longest, s[longest] );
  printf("\n----------------\n\n");
  getchar();  /* to pause */
  return 0;
}
Or you could use fgets();
Last edited by Howard_L : 16-Jul-2009 at 10:08.
  #3  
Old 19-Jul-2009, 09:28
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: How to finish this C Code?


Quote:
Originally Posted by Howard_L
Or you could use fgets();

Yes...whatever that stuff in the [c]...[/c] tags was rather ugly.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    size_t n, l = 0;
    char   buffer[BUFSIZ];
    char   *p, *t, *seps = " \t\n";
    
    printf("Enter a string; use 'end' to terminate: ");
    fgets(buffer, BUFSIZ, stdin);
    
    t = strtok(buffer, seps);
    while(t != NULL)
    {
	if(strcmp(t, "end") == 0)
	{
	    break;
	}
	n = strlen(t);
	if(n > l)
	{
	    p = t;
	    l = n;
	}
	t = strtok(NULL, seps);
    }
    printf("The longest word is %s, which contains %u characters.\n", p, (unsigned)l);
    
    return 0;
}



Output:

Code:
$ ./longeststr Enter a string; use 'end' to terminate: This is not the end of the world as we know it. The longest word is This, which contains 4 characters. $ ./longeststr Enter a string; use 'end' to terminate: Whomsoever reads this will know that the world will eventually come to an end. The longest word is Whomsoever, which contains 10 characters. $ ./longeststr Enter a string; use 'end' to terminate: end The longest word is (null), which contains 0 characters. $ ./longeststr Enter a string; use 'end' to terminate: Superman met Batman at the super heros party where they felt like the world was coming to an end. The longest word is Superman, which contains 8 characters. ./longeststr Enter a string; use 'end' to terminate: Why can't we be friends and just get along until the end of time? The longest word is friends, which contains 7 characters. $ ./longeststr Enter a string; use 'end' to terminate: The longest word is (null), which contains 0 characters.


MxB
  #4  
Old 19-Jul-2009, 10:17
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: How to finish this C Code?


Well Mx, that is nice looking.
I was trying to cram some basic C string mechanics in there since I think it's important and the OP seemed new. But they've probably gone on to Javaland now anyhow so what the hey... At least I got some practice in and was reminded of strtok(). Thanks!
  #5  
Old 22-Jul-2009, 04:51
Pefectionist Pefectionist is offline
New Member
 
Join Date: Jul 2009
Posts: 4
Pefectionist is on a distinguished road

Re: How to finish this C Code?


Sorry to trouble both of you and sorry for being a little late given my drenched internet, but I somehow got my logic all messed up.
I have a good logic, but it never seems to match up with anything C or math.

OK I haven't learnt char yet, so I am not expected to use it and I'll wipe the system("pause") out, thanks for the tip.

What is confusing me is this last bit here
CPP / C++ / C Code:
while((strEqual (input, "end"))!=0) /* I'll be simplifying this, but it seems to work quite well as it is */
{

length = StringLength(input); /*For some reason I'm learning StringLength not strLen so I can't do much about this*/
printf("Enter Next String?\n");
input = GetLine();
}
if (length - 1 > strmax) /* This was a desperate try, I'm sure it makes not sense. So from here on, I am officially lost */
{
length = strmax;
resultStr = StringLength(length);
}

printf("The longest string is: %s\n", resultStr);

I don't know how to fix it without making it a very complicated code which has a 50% chance of working. Maybe I need to go do some meditation to clear my mind up

Thanks!
  #6  
Old 22-Jul-2009, 14:42
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: How to finish this C Code?


Code:
length = StringLength(input); / *For some reason I'm learning StringLength not strLen so I can't do much about this */
Why NOT? You got a delete key???
Is this what you are learning in a class? great......
The class should be teaching you to use standard header files and the functions they provide.
You got no time to be dilly dallyin around with makin up sissy words....

Is this supposed to be C or C++?
There is no standard "string" datatype in C as you have in your first post above.

The files:
#include "simpio.h"
#include "genlib.h"
#include <strlib.h> (did you mean stdlb.h ?)

...are not standard C header files so we don't know anything about the functions they provide.
Same for these functions:
GetLine()
StringLength()

So , we can't help you if you insist on using this stuff.
If you have defined those functions in those files then you should supply us with them too.
You program could easily be written using the functions found in the standard libraries:
#include <stdio.h>
#include <string.h>

In those header files you can find the functions to replace the ones you are trying to use.
strcmp = strEqual()
fgets() = sGetLine()
strlen() = StringLength()

Would you like to try that? THEN we could help...
  #7  
Old 22-Jul-2009, 23:23
Pefectionist Pefectionist is offline
New Member
 
Join Date: Jul 2009
Posts: 4
Pefectionist is on a distinguished road

Re: How to finish this C Code?


Wow *bummed* OK I'll do just that.
  #8  
Old 23-Jul-2009, 10:22
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: How to finish this C Code?


Bummer - NOT , just make the substitutions and move on , your a perfectionist aren't you????.

You will need to use standard C datatypes as well.
Instead of String you will need to declare a C string ( an array of type char ):
Here is a basic outline of one way of doing this for you to think about:
CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
  char str_in[64] = {0}; /* A "C string" array of 64 chars , all initialized to zero */
  char str_longest[64] = {0}; 
  int cur_len, longest = 0;

  for(;;)  <-- start endless loop
  {
    fgets() a str_in;
    strlen() the length into cur_len;
    overwrite the ending \n on the string with a '\0';
    if( (strcmp(str_in , "end") = 0 )    <-- the strings match
    {
      break;
    }   
    if( test to see if cur_len is longer than longest)
    {
      if yes then  strcpy()  str_in to str_longest;
      assogn cur_len to longest;
    }
  }   back to top of loop

  printf( report your results... );

  return 0;
}
YOU will need to fill in the blanks above. Use the refence I posted in previous post.
fgets() is a little tough so here is an example of fgets() usage to keep you interested:
CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
  char s1[64] = {0};
  unsigned int i, j;

  i = sizeof(s1);          /* get the size of the s1 char array */
       printf("Enter a string: (%d chars max) \n", i);
  fgets( s1, i, stdin );   /* get the user input... 
                              fgets() copies the \n of the input to s1 which 
                              stinks... so we get the length - 1 
                              (do the indexing math to understand why)      */
  j = (strlen(s1) - 1 );
  s1[j] = '\0';            /* and overwrite that \n with a zero terminator  */
  printf("i= %d , j= %d , s1 is: <%s> \n", i, j, s1);

  return 0;
}
Output:
Code:
Enter a string: (64 chars max) We hold these truths to be self evident, i= 64 , j= 40 , s1 is: <We hold these truths to be self evident,>
  #9  
Old 29-Jul-2009, 17:51
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: How to finish this C Code?


Quote:
Originally Posted by Howard_L
Same for these functions:
GetLine()
StringLength()

Apparently YOU don't know ANYTHING about abstract art, eh?!


MxB
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble integrating console code into GUI Barman007 Java Forum 18 15-May-2008 14:05
How to sort random access file? wmmccoy0910 C Programming Language 12 04-Sep-2006 04:40
Here it is again! 35% - 40% off For Life! my-e-space Web Hosting Advertisements & Offers 0 20-Apr-2006 15:48
Guidelines for posting requests for help - UPDATED! WaltP C Programming Language 0 21-Apr-2005 03:44

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:43.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.