
Two strings will be in contrast in numerous methods. On this tutorial, first, we’ll see a user-defined perform to match two strings, after which we’ll see some built-in library capabilities which can be utilized to match two strings very simply. So, let’s get began.
String comparability utilizing a user-defined perform :
We are going to write a perform stringCompare() to match strings. We traverse the strings and examine every character of the string till we attain the tip of anyone or each or one mismatched are discovered. If the traversal is reached to the tip of each the strings, then the strings are matched; in any other case, strings are mismatched.
02.
03. #embody
04.
05. int stringCompare( char str1[ ], char str2[ ] )
06.
07. int i=0;
08.
09. whereas( str1[i] == str2[i] )
10.
15.
16. if( str1[i] == ” && str2[i] == ” )
17. return 0;
18. else
19. return -1;
20.
21.
22.
23.
24. int principal()
25.
Right here we traverse the strings utilizing whereas loop and a variable i. When characters are equal in the identical place of each strings, the worth of i is incremented by 1 (line 13). If characters are usually not equal (line 09) or we attain the tip of the string (line 11), then the whereas loop is a break. After the whereas loop, we verify each the string traversals are reached to the tip or not (line 16). If the traversal is reached to the tip of each strings, then the strings are equal in any other case not.
String comparability utilizing built-in library capabilities :
The next library capabilities can be utilized for string comparability. All of the capabilities are declared within the string.h header file.
strcmp() perform :
This perform compares two strings handed to the perform.
Syntax:
Return worth: Return Zero if the strings are equal. Return a unfavorable integer if the ASCII worth of the primary unmatched character of the primary string is lower than the second string. Return a optimistic integer if the ASCII worth of the primary unmatched character of the primary string is bigger than the second string. Some techniques return distinction of the ASCII worth of first mismatched character and a few techniques return -1 if the ASCII worth of the primary unmatched character of the primary string is lower than the second string and return 1 if the ASCII worth of the primary unmatched character of the primary string is bigger than the second string.
Instance | Return Worth | Clarification |
strcmp( “Howdy World”,”Howdy World” ) | 0 | Two strings are the identical. |
strcmp( “Howdy”,”Howdy World” ) | 0 | Strings are in contrast until the character ‘’. The primary string by default ends with ‘’, and the second string accommodates the ‘’ character after ‘Howdy’. |
strcmp( “Howdy″,”Howdy World” ) | 0 | Strings are in contrast until the character ‘’. |
strcmp( “Howdy World”,”hiya World” ) | Damaging integer | ASCII worth of the primary unmatched character of the primary string (‘H’) is lower than the second string (‘h’) |
strcmp(“hiya World”,”Howdy World” ) | Optimistic integer | ASCII worth of the primary unmatched character of the primary string (‘h’) is bigger than the second string (‘H’) |
strncmp() perform :
This perform is just like the perform strcmp(), however right here we’ve to specify what number of bytes are in contrast by passing an additional argument to the perform.
Syntax:
Return worth: The perform returns Zero if the primary n characters of the 2 strings are equal; in any other case, it returns unfavorable or optimistic integer relying on the signal of the variations between the primary mismatched character’s ASCII worth.
Instance | Return Worth | Clarification |
strncmp( “Howdy World”,”Howdy World”,5 ) | 0 | First 5 characters are the identical. |
strncmp( “Howdy”,”Howdy World”,5 ) | 0 | First 5 characters are the identical. |
strncmp( “Howdy″,”Howdy World”,8 ) | 0 | ‘’ is after the primary 5 characters in each strings. So, comparability is stopped after 5 not 8. |
strncmp( “Howdy World”,”hiya World”,5 ) | Damaging integer | ASCII worth of the primary unmatched character of the primary string (‘H’) is lower than the second string (‘h’) |
strcasecmp() perform :
This perform is just like the perform strcmp(), however right here the strings are usually not case delicate.
Syntax:
Return worth: Similar as strcmp(), however strings are handled as case-in-sensitive.
Instance | Return Worth | Clarification |
strcasecmp( “Howdy World”,”Howdy World” ) | 0 | Two strings are the identical. |
strcasecmp( “Howdy”,”Howdy World” ) | 0 | Strings are in contrast until the character ‘’. The primary string by default ends with ‘’, and the second string comprise the ‘’ character after ‘Howdy’. |
strcasecmp( “Howdy World”,”hiya World” ) | 0 | Strings are case-in-sensitive. So, “Howdy World” and “hiya World” are the identical. |
strncasecmp() perform :
This perform is just like the perform strncmp(), however right here the strings are usually not case delicate.
Syntax:
Return worth: Similar as strncmp(), when strings are handled as case-in-sensitive.
Instance | Return Worth | Clarification |
strncasecmp( “Howdy World”,”Howdy World”,5 ) | 0 | First 5 characters are the identical. |
strncasecmp( “Howdy”,”Howdy World”,5 ) | 0 | First 5 characters are the identical. |
strncasecmp( “Howdy″,”Howdy World”,8 ) | 0 | ‘’ is after the primary 5 characters in each strings. So, comparability is stopped after 5 not 8. |
strncasecmp( “Howdy World”,”hiya World”,5 ) | 0 | Strings are case-in-sensitive. So, “Howdy” and “hiya” are the identical. |
memcmp() perform :
This perform compares two reminiscence blocks byte by byte. We have now to move two pointers of the reminiscence blocks and the variety of bytes to match.
Syntax:
Return worth: The perform returns Zero if the 2 reminiscence blocks (n bytes) are equal; in any other case, it returns the variations between the primary mismatched pair of bytes (bytes are interpreted as unsigned char objects, then promoted to int).
Instance | Return Worth | Clarification |
memcmp( “Howdy World”,”Howdy World”,5 ) | 0 | First 5 characters are the identical. |
memcmp( “Howdy″,”Howdy World”,8 ) | Damaging integer | The primary 6 characters are the identical, however the seventh character is totally different. Right here comparability not stopped like strncmp() when getting ‘’ character. |
memcmp( “Howdy World”,”hiya World”,11 ) | Damaging integer | ASCII worth of the primary unmatched character of the primary string (‘H’) is lower than the second string (‘h’) |
Instance:
Following is the C code instance of all of the capabilities mentioned.
02.
03. #embody
04. #embody
05.
06. int principal()
07.
08. printf(“strcmp( “Howdy World”,”Howdy World” ) => %dn”,strcmp( “Howdy World”,”Howdy World” ));
09. printf(“strcmp( “Howdy”,”Howdy\Zero World” ) => %dn”,strcmp( “Howdy”,”Howdy World” ));
10. printf(“strcmp( “Howdy World”,”hiya World” ) => %dn”,strcmp( “Howdy World”,”hiya World” ) );
11. printf(“strcmp( “Howdy\0\0\0″,”Howdy\Zero World” ) => %dn”,strcmp( “Howdy”,”Howdy World” ));
12.
13. printf(“n—————n”);
14.
15. printf(“strncmp( “Howdy World”,”Howdy World”,5 ) => %dn”,strncmp( “Howdy World”,”Howdy World”,5 ));
16. printf(“strncmp( “Howdy”,”Howdy\Zero World”,5 ) => %dn”,strncmp( “Howdy”,”Howdy World”,5 ));
17. printf(“strncmp( “Howdy\0\0\0″,”Howdy\Zero World”,8 ) => %dn”,strncmp( “Howdy”,”Howdy World”,8 ));
18. printf(“strncmp( “Howdy World”,”hiya World”,5 ) => %dn”,strncmp( “Howdy World”,”hiya World”,5 ));
19.
20. printf(“n—————n”);
21.
22. printf(“strcasecmp( “Howdy World”,”Howdy World” ) => %dn”,strcasecmp( “Howdy World”,”Howdy World” ));
23. printf(“strcasecmp( “Howdy”,”Howdy\Zero World” ) => %dn”,strcasecmp( “Howdy”,”Howdy World” ));
24. printf(“strcasecmp( “Howdy World”,”hiya World” ) => %dn”,strcasecmp( “Howdy World”,”hiya World” ));
25.
26. printf(“n—————n”);
27.
28. printf(“strncasecmp( “Howdy World”,”Howdy World”,5 ) => %dn”,strncasecmp( “Howdy World”,”Howdy World”,5 ) );
29. printf(“strncasecmp( “Howdy”,”Howdy\Zero World”,5 ) => %dn”,strncasecmp( “Howdy”,”Howdy World”,5 ));
30. printf(“strncasecmp( “Howdy\0\0\0″,”Howdy\Zero World”,8 ) => %dn”,strncasecmp( “Howdy”,”Howdy World”,8 ));
31. printf(“strncasecmp( “Howdy World”,”hiya World”,5 ) => %dn”,strncasecmp( “Howdy World”,”hiya World”,5 ));
32.
33. printf(“n—————n”);
34.
35. printf(“memcmp( “Howdy World”,”Howdy World”,5 ) => %dn”,memcmp( “Howdy World”,”Howdy World”,5 ) );
36. printf(“memcmp( “Howdy\0\0\0″,”Howdy\Zero World”,8 ) => %dn”,memcmp( “Howdy”,”Howdy World”,8 ));
37. printf(“memcmp( “Howdy World”,”hiya World”,11 ) => %dn”,memcmp( “Howdy World”,”hiya World”,11 ));
38.
39. return 0; 40.
Conclusion:
So, on this tutorial, we’ve seen how strings will be in contrast in numerous methods. As we’ve seen, the stringCompare() perform returns -1 for unequal strings, however this may be modified in order that it returns ASCII worth of mismatched character. You should utilize it in your code, which is finest suited to you.
string compare in c without using strcmp,strcmp vs strncmp,how to compare two strings in c++,atoi full form,strcmpi in c,implicit declaration of function 'strcmp,flowchart to compare two strings,how to compare two characters in c++,what is the size of unsigned char,how to compare characters in a string in c,strncmp in c,full name of atoi,strcmp() function in c++,if char c,c strncmp,how to compare characters in a string in c++,how to compare characters in a string in java,compare two strings in c++,what is an array,online c compiler,program to compare two strings in python,in c programming a function can return,what is similar to interface in c++,which statement is true about function,strcmp in c++,string compare in c++,c strcmp namespace,what is the need for functions in c,string programs in c c4learn,two strings 3,comparing two strings in c,strcmp in c,string copy in c,c program to compare two strings without using strcmp using pointers,strcpy in c