Comparing Two Strings

This is a discussion on Comparing Two Strings within the C Programming forums, part of the General Programming Boards category; WHAT I GOTTA DO: Write a program that uses the C string library strncmp() function to compare two strings input ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    24

    Comparing Two Strings

    WHAT I GOTTA DO:

    Write a program that uses the C string library strncmp() function to compare two strings input by the user. The number of characters to be compared should be the length of the shortest string inputted (you may assume an input string will always be 2 or more characters). The program should state whether the first input string is less than, equal to, or greater than the second input string.

    Test your solution with the following test data:

    aab
    aabb //should be equal

    abcde
    abcdf // second string should be greater

    abcdefF
    abcdeF // first string should be greater


    WHAT I GOT SO FAR:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char String1[80];
    	char String2[80];
    	
    	printf("Input the first string:\n\n");
    
    	gets(String1);
    
    	printf("\nInput the second string:\n\n");
    
    	gets(String2);
    
    	printf("\nCompare strings:\n");
    	printf("                %s\n", String1);
    	printf("                %s\n\n", String2);
    	printf("Result:         %s\n", strncmp(String1,String2));  
    	
    	for strncmp(String1,String2){
    		if (strncmp(String1,String2) == 0){
    			printf("String 1 is equal to String 2");
    		}
    		else if (strncmp(String1,String2) == 1){
    			printf("String 1 is greater than String 2");
    		}
    		else{
    			printf("String 1 is less than String 2");
    		}
    	}
    
    	return 0;
    	
    }
    6 errors

    : error C2198: 'strncmp' : too few actual parameters
    : error C2061: syntax error : identifier 'strncmp'
    : error C2059: syntax error : 'else'
    : error C2059: syntax error : 'else'
    : error C2059: syntax error : '}'
    : error C2059: syntax error : '}'
    Error executing cl.exe.
    Last edited by Sure; 06-26-2005 at 02:25 PM.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You should not use gets() - read the faq as to why you don't want to do that.

    Also, are you sure you wish to use the strncmp() function? You have not passed enough arguments to it. The way you are using it makes me wonder if you want the strcmp() function.
    Last edited by kermit; 06-26-2005 at 02:28 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    i have to use the gets() and the strncmp() functions in this program....

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Sure
    i have to use the gets() and the strncmp() functions in this program....
    Well it is unfortunate that your teacher has made you do this - do it for the assignment, but know that gets() is a very dangerous function to use. Even the UNIX man pages say not to use it:

    BUGS
    Never use gets(). Because it is impossible to tell without knowing the
    data in advance how many characters gets() will read, and because
    gets() will continue to store characters past the end of the buffer, it
    is extremely dangerous to use. It has been used to break computer
    security. Use fgets() instead.

    It is not advisable to mix calls to input functions from the stdio
    library with low - level calls to read() for the file descriptor asso-
    ciated with the input stream; the results will be undefined and very
    probably not what you want.
    If you must use strncmp() that is fine - but read the documentation on how to implement it properly in your program.

    ~/

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    114
    You missing a parameter to strncmp() as the error says. Heres a hint: "The number of characters to be compared should be the length of the shortest string inputted (you may assume an input string will always be 2 or more characters)."

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Also, this is not right:

    Code:
    printf("Result:         %s\n", strncmp(String1,String2));
    strncmp() returns an integer, not a string.

    ~/

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    thx for the ideas ill give it a shot

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    well still cant figure it out.....

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Can you post your most recent code?

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    its basically the same , jus cahnged %s to %d and thats all i know ... the text isnt even helpin

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You need to pass a third argument to strncmp() - this argument will be the number of characters you want compared. So for example, in your first example,

    aab
    aabb //should be equal
    Given the specs, the value passed as the third argument would be 3, as the shortest string (aab) is three characters long. If you compare the first three characters of the second string (aabb) to the characters of the first string (aab), you will find that you have a match. In this case then, the return value from strncmp would be 0.

    When you call strncmp() it would look something like this:

    Code:
    if (strncmp(String1,String2, n) == 0){
    where n would be the amount of characters in the shortest string.
    Last edited by kermit; 06-26-2005 at 06:48 PM.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    so i could use strlen() to determine the length of the string?

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    That would be one way of doing it, yes. If you are allowed, that might be your easiest bet.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    Well got it to work without errors but it dosnt pass the tests

    Test your solution with the following test data:

    aab
    aabb //should be equal

    abcde
    abcdf // second string should be greater

    abcdefF
    abcdeF // first string should be greater

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char s1[80];
    	char s2[80];
    	
    
    	printf("Input the first string:\n\n");
    
    	gets(s1);
    
    	printf("\nInput the second string:\n\n");
    
    	gets(s2);
    
    	printf("\nCompare strings:\n");
    	printf("\t\t%s\n", s1);
    	printf("\t\t%s\n\n", s2);
    	                           
    	if (strncmp(s1,s2,21) == 0)
    		printf("Result:\t\tString 1 is equal to String 2\n\n");
    	
    	if (strncmp(s1,s2,21) == 1)
    		printf("Result:\t\tString 1 is greater than String 2\n\n");
    	
    	if (strncmp(s1,s2,21) == -1)
    		printf("Result:\t\tString 1 is less than String 2\n\n");
    
    	return 0;
    	
    }

  15. #15
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    this site might help.
    http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
    below is an excerpt from the site
    ==
    !=
    <
    >
    <=
    >=

    The comparison operators return a Boolean (true/false) value indicating whether the specified relationship exists between the two operands. The operands may be:
    two string objects
    a string object and a character string literal
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing 2 strings with pointer
    By meriororen in forum C Programming
    Replies: 9
    Last Post: 05-22-2009, 08:37 PM
  2. Replies: 2
    Last Post: 04-29-2009, 11:13 AM
  3. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  4. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 10:20 AM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 11:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21