Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with the help of string function, i.e., strcmp(), which is defined in a string.h header file.
The string function which is pre-defined in a string.h header file is a strcmp() function. The strcmp() function consider two strings as a parameter, and this function returns an integer value where the integer value can be zero, positive or negative.
int strcmp (const char* str1, const char* str2);
In the above syntax, two parameters are passed as strings, i.e., str1 and str2, and the return type is int means that the strcmp() returns an integer value.
The strcmp() function compares the character of both the strings. If the first character of both the strings are same, then this process of comparison will continue until all the characters are compared or the pointer points to the null character '\0'.
Possible return values from the strcmp() function
Return value | Description |
---|---|
0 | When both the strings are equal. |
<0 | If the ASCII value of a character of the first string is less than the ASCII value of a character of the second string, then the function will return negative value. |
>0 | If the ASCII value of a character of the first string is greater than the ASCII value of a character of the second string, then the function will return positive value. |
Let's understand through an example.
#include #include int main() { char str1[20]; // declaration of char array char str2[20]; // declaration of char array int value; // declaration of integer variable printf("Enter the first string : "); scanf("%s",str1); printf("Enter the second string : "); scanf("%s",str2); // comparing both the strings using strcmp() function value=strcmp(str1,str2); if(value==0) printf("strings are same"); else printf("strings are not same"); return 0; }
#include int compare(char[],char[]); int main() { char str1[20]; // declaration of char array char str2[20]; // declaration of char array printf("Enter the first string : "); scanf("%s",str1); printf("Enter the second string : "); scanf("%s",str2); int c= compare(str1,str2); // calling compare() function if(c==0) printf("strings are same"); else printf("strings are not same"); return 0; } // Comparing both the strings. int compare(char a[],char b[]) { int flag=0,i=0; // integer variables declaration while(a[i]!='\0' &&b[i]!='\0') // while loop { if(a[i]!=b[i]) { flag=1; break; } i++; } if(flag==0) return 0; else return 1; }
#include int stringcompare(char*,char*); int main() { char str1[20]; // declaration of char array char str2[20]; // declaration of char array printf("Enter the first string : "); scanf("%s",str1); printf("\nEnter the second string : "); scanf("%s",str2); int compare=stringcompare(str1,str2); // calling stringcompare() function. if(compare==0) printf("strings are equal"); else printf("strings are not equal"); return 0; } // Comparing both the strings using pointers int stringcompare(char *a,char *b) { int flag=0; while(*a!='\0' && *b!='\0') // while loop { if(*a!=*b) { flag=1; } a++; b++; } if(flag==0) return 0; else return 1; }
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.