This blog is under construction

Sunday, 29 April 2012

strncmp example in C

 Header file:
    string.h

Synopsis:
   int strncmp(char *str1, char *str2, int n);

Description:
   It compares the first n characters of string str1 with the string str2.  Returns 0 if str1 == str2, return < 0 if str1 < str2, return>0 if str1 > str2.


strncmp function C example:


  #include<stdio.h>
  #include<string.h>
  #include<stdlib.h>

  int main() {
        char a[100], b[100];
        int ret;
        printf("Enter your input1:");
        fgets(a, 100, stdin);
        printf("Enter your input2:\n");
        fgets(b, 100, stdin);

        ret = strncmp(a, b, 10);
        if (ret == 0)
                printf("Both are same\n");
        else
                printf("Not same\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/cpgms/chap5$ ./a.out
  Enter your input1:String2
  Enter your input2:String!
  Not same


No comments:

Post a Comment