This blog is under construction

Sunday, 29 April 2012

strncmp example in C

 Header file:

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

   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:


  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");
                printf("Not same\n");
        return 0;

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

No comments:

Post a Comment