String Comparison in C Language

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

In C language, string comparison is the process of comparing two strings to check if they are equal or not. The strcmp() function is used to compare two strings.

The strcmp() function is implemented in the <string.h> library. It compares two strings character by character and returns:

  • 0 if both strings are identical.
  • A positive value if the first string is lexicographically greater than the second string.
  • A negative value if the first string is lexicographically less than the second string.

Here is an example of how to use the strcmp() function:

Copy code

#include <string.h> int main() {    char str1[] = "Hello";    char str2[] = "world";    int result = strcmp(str1, str2);    if(result == 0) {        printf("Both strings are identical.");    } else if(result > 0) {        printf("String 1 is lexicographically greater than String 2.");    } else {        printf("String 1 is lexicographically less than String 2.");    }    return 0; }

In the above example, the output will be "String 1 is lexicographically less than String 2."

It's important to note that strcmp() function is case-sensitive, meaning that "Hello" and "hello" are considered as different strings. If you want to perform case-insensitive string comparison, you can use the strcasecmp() function.

It's also worth noting that strcmp() function is not secure and should be avoided if you are working with strings that come from an untrusted source.

10. String In C

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?