Thread: help needed

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    help needed

    i am so confused about those two questions. can some one give me a hint or two. appreciate

    1.Write a function max that has two string parameters and returns the larger of the two, that is, a string should be returned. (By larger we mean alphabetically, thus the string "b" is greater then the string "apple". The lengths of the two strings are not being compared.)

    2.Write a function min that has three string parameters and returns the smallest.

    for number 2 i come up something => min ("a","b","c",)

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by striped bass
    i am so confused about those two questions. can some one give me a hint or two. appreciate

    1.Write a function max that has two string parameters and returns the larger of the two, that is, a string should be returned. (By larger we mean alphabetically, thus the string "b" is greater then the string "apple". The lengths of the two strings are not being compared.)

    2.Write a function min that has three string parameters and returns the smallest.

    for number 2 i come up something => min ("a","b","c",)
    use strcmp function for the first question to compace two string

    hint on fucntion strcmp
    Code:
    string1 > string2 ==> returs > 0
    string1 == string2 ==> retutn 0
    string1 < string2 ==> return < 0
    and for the second one strcmp to compare string again

    ssharish2005

  3. #3
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Well, strcmp() does what your number 1 should, more or less, altho I suppose you should implement it "from scratch". You should google the ascii table - chars in C follow it. Hint nr. 2: Chars are numeric, so you can do arithmetics on them, and also compare them.

    Your function would look something like this:
    Code:
    char* max(char* foo, char* bar) {
    
    //get length of both strings
    
    //compare first character of both strings
    //if one is bigger, return that string.
    //else they are equal, compare the second character from both
    //provided both strings have one. (that's why you want the length of both strings)
    //loop
    
    }
    For the second function, much the same, only with three strings.
    -S

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Hint 1: use strcmp.

    Hint 2: make an attempt at assignments and post your attempt before coming here for help.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    for the first one i got
    Code:
    max(s1,s2);
    {
    if (strcmp("s1", "S2")>0)
      returns "s1";
     else if (strcmp("s1", "S2")>0) 
        return "s2";
    }
    however, still not correct.... wondering why?

    for the second one :
    Code:
    if (strcmp(name1, name2)<=0 && strcmp(name1, name3)<=0)
    min = name1;
    if(strcmp(name2, name1)<=0 && strcmp(name2, name3)<=0)
    min = name2;
    if (strcmp(name3, name1)<=0 && strcmp(name3, name2)<=0)
    min = name3;
    ...... still not correct!!!

    appreciate for the previousi hint.. wish this time i can get it right.

  6. #6
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    1. s2 and S2 are not the same, C is case sensitive.
    2. s1 is a variable name, "s1" is the string of two characters 's' and '1' with a \0 at the end.
    3. strcmp() returns a value that tells you which string is bigger, or if they are equal - so no need for a second call to it.

    As for your second function, if you compare two strings, you know which of them is smallest. Then you need but compare it to the third string, so you don't need more than two calls to strcmp().

    edit: Oh, and it would be
    Code:
    char* max(s1, s2) { /*code*/ }
    Question: Did you even try to compile that which you posted here?
    Last edited by Silfer; 12-05-2005 at 09:48 PM.
    -S

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Help Please
    By Moose112 in forum C++ Programming
    Replies: 9
    Last Post: 12-10-2006, 07:16 PM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM