The standard C library provides several functions to compare two strings to see whether they are the same. One of these functions, strcmp(), is used here to show how this task is accomplished: #include <stdio.h> #include <string.h> void main(void); void main(void){ char* str_1 = "abc"; char* str_2 = "abc"; char* str_3 = "ABC"; if (strcmp(str_1, str_2) == 0) printf("str_1 is equal to str_2.n"); else printf("str_1 is not equal to str_2.n"); if (strcmp(str_1, str_3) == 0) printf("str_1 is equal to str_3.n"); else printf("str_1 is not equal to str_3.n");} This program produces the following output:str_1 is equal to str_2.str_1 is not equal to str_3.Notice that the strcmp() function is passed two arguments that correspond to the two strings you want to compare. It performs a case-sensitive lexicographic comparison of the two strings and returns one of the following values:Return Value Meaning<0 - The first string is less than the second string.0 - The two strings are equal.>0 - The first string is greater than the second string.In the preceding example code, strcmp() returns 0 when comparing str_1 (which is "abc") and str_2 (which is "abc"). However, when comparing str_1 (which is "abc") with str_3 (which is "ABC"), strcmp() returns a value greater than 0, because the string "ABC" is greater than (in ASCII order) the string "abc".Many variations of the strcmp() function perform the same basic function (comparing two strings), but with slight differences. The following table lists some of the functions available that are similar to strcmp():Function Name Descriptionstrcmp() - Case-sensitive comparison of two stringsstrcmpi() - Case-insensitive comparison of two stringsstricmp() - Same as strcmpi()strncmp() - Case-sensitive comparison of a portion of two stringsstrnicmp() - Case-insensitive comparison of a portion of two stringsLooking at the example provided previously, if you were to replace the call to strcmp() with a call to strcmpi() (a case-insensitive version of strcmp()), the two strings "abc" and "ABC" would be reported as being equal.
C
Topic: Strings
How can you tell whether two strings are the same?
Browse random answers:
How to compare two strings in c ?
What is the difference between System.String and System.StringBuilder classes?
How would you implement a sbstr()function that extract a sub string from a givn string?
How can i print a?%? character in a print format string?
Explain one method to process an entire string as one unit?
Can we use string in switch statement?
What is the difference between strings and character arrays ?
How do you print only part of a string?
How to type a string without using printf function?
How can I convert a number to a string?
How can I convert a string to a number?
What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?
What is the difference between a string and an array?
How to get string in files and print the string in the reverse order?
If the two strings are identical, then strcmp() function returns?
What is the difference between the functions strdup and strcpy in C?
What do you mean by String objects are immutable?
How do you create empty strings in C?
Write a scanf statement which can store one line of string which includes white space?
What will be output when you will execute following c code? #include<stdio.h> void main(){ char arr[7]="Network"; printf("%s",arr); }
Which of the following function sets first n characters of a string to a given character?
What will be the output of the program ? #include<stdio.h>#include<string.h> int main(){ char str1[20] = "Hello", str2[20] = " World"; printf("%sn", strcpy(str2, strcat(str1, str2))); return 0; }
Without using any semicolon (;) in program write a c program which output is: HELLO WORLD?
How can I remove the trailing spaces from a string?
How can I right-justify a string?
How can you tell whether two strings are the same?
How do you print only part of a string?
Is there a way to switch on strings?
How would you use the functions fseek(), freed(), fwrite() and ftell()?
Difference between strdup and strcpy?
What are strings and how can they be represented in c language?
How can you tell whether two strings are the same?
Difference between strdup and strcpy?
How do I use c-strings to write a c program that Removes multiple spaces between words in a paragraph?
What the advantages of using Unions?
String Processing --- Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba.?
Can we convert an unsigned long integer value to a string?
What about strpbrk( )?
What about ceil( ) and floor( )?
Why is it not possible to scan strings from keyboard in case of array of pointers to string?