Thread: Comparing strings using strncmp() function in C language?

  1. #1
    Banned
    Join Date
    Apr 2011
    Posts
    56

    Comparing strings using strncmp() function in C language?

    Hello everyone,

    I was working on a program which accepts 2 strings from the keyboard and the compared n characters from both strings. If n characters from each string equals then print out message saying that else print message saying otherwise. I know that I can use strncmp() function here, and also I need a loop to make a decision. It sounds simple but for some reason I cant make it work. Here is my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char string1[20], string2[20];
    	int n, result = 0;
    
    	printf("\nEnter String 1:");
    		gets(string1);
    	printf("\nEnter String 2:");
    		gets(string2);
    	printf("\nEnter the n:");
    		scanf("%d", &n);
    
    	int result = strncmp(string1,string2,n);  // strncmp() function
    	     if (result > 0)  // I am not sure what I am doing there
    		  printf("\nMatch");
    	     else
    		  printf("\nNot a Match");
    Please help me with this code
    Thank you

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Don't use gets! You've been warned about this before. Fix your dirty, nasty habit, or else I refuse to help you. If you haven't yet, read why gets is bad and how to fix it here (that red text is a link -- click it).

  3. #3
    Banned
    Join Date
    Apr 2011
    Posts
    56
    wow .. you need to calm down sir. Sry about that, and I just upgraded my code.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char string1[20], string2[20];
    	int n, result = 0;
    
    	printf("\nEnter String 1:");
    		scanf("%s",string1);
    	printf("\nEnter String 2:");
    		scanf("%s",string2);
    	printf("\nEnter the n:");
    		scanf("%d", &n);
    
    	int result = strncmp(string1,string2,n);  // strncmp() function
    	     if (result > 0)  // I am not sure what I am doing there
    		  printf("\nMatch");
    	     else
    		  printf("\nNot a Match");

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I will be as calm or uncalm as I please, thank you very much. And since the gentle suggestions in the past didn't seem to work, I thought I would step it up a notch. Besides, it's for your own good -- consider it a little tough love.

    scanf is okay for some things, but it will only read up to the first white space character, so you only get one word. That may be fine for what you need, but I'm guessing not in this case. The better solution would be:
    Code:
    fgets(string1, sizeof(string1), stdin);
    Now, you should really, really read the docs for strncmp more carefully. If you're on Linux, "man strcmp" will work, or you can check your compiler help files and there's always Google:
    Quote Originally Posted by man strncmp
    RETURN VALUE
    The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less
    than, to match, or be greater than s2.
    You only want to report a match if strncmp returns 0. Go ahead and change that to "if (result == 0)" and you should get better results.

  5. #5
    Banned
    Join Date
    Apr 2011
    Posts
    56
    ah thanks it works and I love u too lol

    jk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-04-2009, 01:36 AM
  2. strncmp like function in C#
    By x77 in forum C# Programming
    Replies: 4
    Last Post: 11-18-2007, 06:29 AM
  3. [HELP] about comparing between two strings...
    By kyaky in forum C++ Programming
    Replies: 12
    Last Post: 08-06-2007, 10:12 AM
  4. Comparing strings
    By manutd in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2006, 06:49 PM
  5. need function like strncmp(str1, str2, n)
    By evader in forum C Programming
    Replies: 5
    Last Post: 09-23-2001, 08:42 PM