Thread: please help me with Strings problem

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Question please help me with Strings problem

    Hello everyone!
    Please help me!
    This is my problem:
    I have to ask the user to enter two strings. Then I have to have a statement that analyzes those two strings and say which one is longer. I have to use the functions----lencmp() and longer()
    I have to use a loop in the main program which compares the lengths of the two string pairs entered by the user. and use gets() to read string data. Please help me if you can, with some real parts of code that will make the program ACTUALLY work. : )
    Thanks very much!
    Sue

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Start with this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    .........
    Join Date
    Nov 2002
    Posts
    303
    I'm not familiar with longer() and lencmp(), are these functions you have to make? Post what you have so far.
    Last edited by SourceCode; 04-03-2003 at 03:32 PM.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    yes they are functions i have to make. this is the problem:
    Which is longer? (p:546 q:3)
    a. Write a function lencmp() that has two string parameters. It should return 1 if the first string is longer than the second, 0 if they are the same length, and –1 if the second is longer.
    b. Write a function longer() , that has two string parameters and return a value of type string. It should call lencmp() to compare the lengths of the two strings and return a pointer to the longer one.
    c. Write a main program with a loop which compares the lengths of 3 string pairs entered by the user.
    Use gets() to read string data.
    Following are the prototypes for the required functions:

    int lencmp (char[ ], char[ ]);
    char * longer (char[ ], char[ ]);

    When you run your program, the screen view will look like the following:

    Enter the first string : Detroit Pistons
    Enter the second string : Sacramento Kings
    Sacramento Kings is longer.

    Enter the first string : Detroit Pistons
    Enter the second string : Chicago Bulls
    Detroit Pistons is longer.
    THIS IS WHAT I HAVE SO FAR:

    #include <stdio.h>
    int lencmp(char[]);
    int main()
    {

    char firststring[15];
    char secondstring[30];
    char *longer(char[], char[]);
    int size;

    puts("Enter your first string:");
    gets(firststring);

    puts("Enter your second string:");
    gets(secondstring);
    size=lencmp(firststring);
    /*printf("Your first string includes %i chars\n", size);*/
    size=lencmp(secondstring);
    /*printf("Your second string includes %i chars\n", size);*/
    printf("%s\n,longer);
    return 0;
    }

    int lencmp(char sentence[])

    {

    int size;
    size=strlen(sentence);

    return size;
    }
    IGNORE what is between the /* that's the program that tells you how many characters each string is but I need to just print out which one is longer. please help!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Sue, read this , then edit your post to add the code tags.

    >...and return a value of type string
    Oh dear, there's no such thing a type string in C

    >Use gets() to read string data.
    Oh dear (again). This is in the FAQ, you should really use fgets() which is buffer safe.

    >printf("%s\n,longer);
    This has a " (quote) missing.
    >printf("%s\n",longer);

    >size=strlen(sentence);
    For strlen(), you'll need to include <string.h>

    >char firststring[15];
    >char secondstring[30];
    Why are they two different lengths?

    >int lencmp(char sentence[])
    Your function prototype doesn't match the requirement of:
    >int lencmp (char[ ], char[ ]);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    Yeah i still don't understand. Maybe i'm just stupid. who knows. I wish this stuff wasn't so hard. we have to use gets() and not anything else. I know my source code is wrong, i can't get anything to work and all the computer science stuff i read is like mumbo-jumbo to me. i just don't understand it and i read and re-read the chapter over and over again! can't i just pay someone to do my lab homework?
    sue

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>Yeah i still don't understand
    what you don't understand people here explained a lot, if you still need more help, ask exactly what you dont understand.

    >>we have to use gets() and not anything else
    what is this you have? so you're doing a big mistake knowing about his existance? haha, I hate this kind of "we have". those teachers kill me.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>int lencmp (char[ ], char[ ]);
    For this function, simply call strlen() for each of the two strings, storing the result in two ints.

    >length1 = strlen(s1);

    Then compare the two length variables, and make the appropriate return.

    if (length1 < length2)
    ...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    oh i see what you mean now hammer
    by comparing the two variables. so i am going to call my first string s1, that would be ok? then call the second string s2 then compared the two in a loop? how do i tell it to do it three times? with a for loop?
    sue

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Why would you be allowed to use string.h for this sort of assignment?
    Code:
    int lencmp(char a[], char b[])
    {
        int result;
    
        if(strlen(a) > strlen(b))
            result = 1;
         else if(strlen(a) == strlen(b))
            result = 0;
         else 
             result = -1;
    
         return result;
    }
    Whadda bout?

    Code:
    int lencmp(char a[], char b[])
    {
        int i, j, result;
    
        i=j=0;
    
        while(a[i] != '\0')
            i++;
         while(b[j] != '\0')
            j++;
    
         if(i > j ) result = 1;
         else if(i == j) result = 0;
         else result = -1;
    
         return result;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    wow!
    now i'm really confused! i'm going to try the first one you stated ronin, the second looks a little too advanced for me and my class and our prof will think we are ahead of him! ok, thanks!

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    This is what I have now:
    but it's still wrong

    #include <stdio.h>
    int lencmp(char[],char[]);
    int main()
    {

    char a[30];
    char b[30];

    int size;

    puts("Enter your first string:");
    gets(a);

    puts("Enter your second string:");
    gets(b);

    result=lencmp(a);

    result=lencmp(b);


    return 0;
    }

    int lencmp(char [],char[])
    {
    int result;

    if(strlen(a) > strlen(b))
    result = 1;
    else if(strlen(a) == strlen(b))
    result = 0;
    else
    result = -1;

    return result;
    }

  13. #13
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Give this a try

    Code:
    #include <stdio.h> 
    #include <string.h>
    
    int lencmp(char a[], char b[])
    {
        int result;
    
        if(strlen(a) > strlen(b))
            result = 1;
         else if(strlen(a) == strlen(b))
            result = 0;
         else 
             result = -1;
    
         return result;
    }   
    
    int main()
    {
        char str1[30];
        char str2[30];
    		
        puts("Enter your first string:");
        gets(str1);
    	
        puts("Enter your second string:");
        gets(str2);
    			
        printf("result: %d", lencmp(str1, str2));
    	
       return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  14. #14
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Oops. I should have explained a bit about your latest code.

    You need #include <string.h> when using strlen

    You didn't set up the lencmp definition correctly

    int lencmp(char [],char []); /* prototype */
    int lencmp(char s1[], char s2[]){...} /* definition */

    I coded the definition before main so that I didn't have to code the prototype. That's why the prototype is missing in my reply.

    Lastly, you didn't call lencmp correctly. You'd need something like

    size = lencmp(a, b);
    printf("size is: %d", size);

    HTH
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM