This blog is under construction

Sunday 29 April 2012

strcmp example in C

Header file:
    string.h

Synopsis:
    int strcmp(char *str1, char *str2);

Description:
     It compares string str1 with str2 and return 0 if str1 == str2, return < 0 if str1 < str2, return > 0 if str1 > str2.


strcmp 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 = strcmp(a,b);
        if (ret == 0)
                printf("Result: Both are same\n");
        else
                printf("Result: Not same\n");
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input1:
  comparing strings
  Enter your input2:
  comparing strings
  Result: Both are same



No comments:

Post a Comment