A string in C language is an array of characters, which is terminated with a null character (). Using this property strings are in distinction.String for comparison: c ProgrammingTwo strings will likely be in distinction in quite a few strategies. On this tutorial, first, we’ll see a user-defined carry out to match two strings, after which we’ll see some built-in library capabilities which could be utilized to match two strings very merely. So, let’s get started.

String comparability using a user-defined carry out :

We’re going to write a carry out stringCompare() to match strings. We traverse the strings and study each character of the string until we attain the tip of anybody or every or one mismatched are found. If the traversal is reached to the tip of every the strings, then the strings are matched; in every other case, strings are mismatched.

01. /*userDefinedFunction.c*/
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.

String for comparison: c Programming

Proper right here we traverse the strings using whereas loop and a variable i. When characters are equal within the similar place of every strings, the value of i is incremented by 1 (line 13). If characters are often 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 confirm every the string traversals are reached to the tip or not (line 16). If the traversal is reached to the tip of every strings, then the strings are equal in every other case not.

String comparability using built-in library capabilities :

The following library capabilities could be utilized for string comparability. The entire capabilities are declared inside the string.h header file.

strcmp() carry out :

This carry out compares two strings handed to the carry out.

Syntax:

int strcmp(const char *str1, const char *str2)

Return value: Return Zero if the strings are equal. Return a unfavorable integer if the ASCII value of the first unmatched character of the first string is decrease than the second string. Return a optimistic integer if the ASCII value of the first unmatched character of the first string is greater than the second string. Some methods return distinction of the ASCII value of first mismatched character and some methods return -1 if the ASCII value of the first unmatched character of the first string is decrease than the second string and return 1 if the ASCII value of the first unmatched character of the first string is greater than the second string.

Occasion Return Price Clarification
strcmp( “Howdy World”,”Howdy World” ) 0 Two strings are the similar.
strcmp( “Howdy”,”Howdy World” ) 0 Strings are in distinction till the character ‘’. The first string by default ends with ‘’, and the second string accommodates the ‘’ character after ‘Howdy’.
strcmp( “Howdy″,”Howdy World” ) 0 Strings are in distinction till the character ‘’.
strcmp( “Howdy World”,”hiya World” ) Damaging integer ASCII value of the first unmatched character of the first string (‘H’) is decrease than the second string (‘h’)
strcmp(“hiya World”,”Howdy World” ) Optimistic integer ASCII value of the first unmatched character of the first string (‘h’) is greater than the second string (‘H’)

strncmp() carry out :

This carry out is rather like the carry out strcmp(), nonetheless proper right here we have to specify what variety of bytes are in distinction by passing a further argument to the carry out.

Syntax:

int strncmp(const char *str1, const char *str2, size_t n)

Return value: The carry out returns Zero if the first n characters of the two strings are equal; in every other case, it returns unfavorable or optimistic integer counting on the sign of the variations between the first mismatched character’s ASCII value.

Occasion Return Price Clarification
strncmp( “Howdy World”,”Howdy World”,5 ) 0 First 5 characters are the similar.
strncmp( “Howdy”,”Howdy World”,5 ) 0 First 5 characters are the similar.
strncmp( “Howdy″,”Howdy World”,8 ) 0 ‘’ is after the first 5 characters in every strings. So, comparability is stopped after 5 not 8.
strncmp( “Howdy World”,”hiya World”,5 ) Damaging integer ASCII value of the first unmatched character of the first string (‘H’) is decrease than the second string (‘h’)

strcasecmp() carry out :

This carry out is rather like the carry out strcmp(), nonetheless proper right here the strings are often not case delicate.

Syntax:

int strcasecmp(const char *str1, const char *str2)

Return value: Comparable as strcmp(), nonetheless strings are dealt with as case-in-sensitive.

Occasion Return Price Clarification
strcasecmp( “Howdy World”,”Howdy World” ) 0 Two strings are the similar.
strcasecmp( “Howdy”,”Howdy World” ) 0 Strings are in distinction till the character ‘’. The first 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 similar.

strncasecmp() carry out :

This carry out is rather like the carry out strncmp(), nonetheless proper right here the strings are often not case delicate.

Syntax:

int strncasecmp(const char *str1, const char *str2)

Return value: Comparable as strncmp(), when strings are dealt with as case-in-sensitive.

Occasion Return Price Clarification
strncasecmp( “Howdy World”,”Howdy World”,5 ) 0 First 5 characters are the similar.
strncasecmp( “Howdy”,”Howdy World”,5 ) 0 First 5 characters are the similar.
strncasecmp( “Howdy″,”Howdy World”,8 ) 0 ‘’ is after the first 5 characters in every 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 similar.

memcmp() carry out :

This carry out compares two memory blocks byte by byte. Now we have now to maneuver two pointers of the memory blocks and the number of bytes to match.

Syntax:

int memcmp(const void *str1, const void *str2, size_t n)

Return value: The carry out returns Zero if the two memory blocks (n bytes) are equal; in every other case, it returns the variations between the first mismatched pair of bytes (bytes are interpreted as unsigned char objects, then promoted to int).

Occasion Return Price Clarification
memcmp( “Howdy World”,”Howdy World”,5 ) 0 First 5 characters are the similar.
memcmp( “Howdy″,”Howdy World”,8 ) Damaging integer The first 6 characters are the similar, nonetheless the seventh character is completely totally different. Proper right here comparability not stopped like strncmp() when getting ‘’ character.
memcmp( “Howdy World”,”hiya World”,11 ) Damaging integer ASCII value of the first unmatched character of the first string (‘H’) is decrease than the second string (‘h’)

Occasion:

Following is the C code occasion of the entire capabilities talked about.

01. /*stringCompare.c*/
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.

String for comparison: c Programming

Conclusion:

So, on this tutorial, we have seen how strings will likely be in distinction in quite a few strategies. As we have seen, the stringCompare() carry out returns -1 for unequal strings, nonetheless this can be modified so that it returns ASCII value of mismatched character. It is best to put it to use in your code, which is most interesting suited to you.

string examine in c with out utilizing strcmp,strcmp vs strncmp,easy methods to examine two strings in c++,atoi full kind,strcmpi in c,implicit declaration of perform ‘strcmp,flowchart to match two strings,easy methods to examine two characters in c++,what’s the measurement of unsigned char,easy methods to examine characters in a string in c,strncmp in c,full identify of atoi,strcmp() perform in c++,if char c,c strncmp,easy methods to examine characters in a string in c++,easy methods to examine characters in a string in java,examine two strings in c++,what’s an array,on-line c compiler,program to match two strings in python,in c programming a perform can return,what’s just like interface in c++,which assertion is true about perform,strcmp in c++,string examine in c++,c strcmp namespace,what’s the want for features in c,string applications in c c4learn,two strings 3,evaluating two strings in c,strcmp in c,string copy in c,c program to match two strings with out utilizing strcmp utilizing pointers,strcpy in c

Share: