Compare two strings in programming language C.

Author: Judy Howell
Date Of Creation: 27 July 2021
Update Date: 1 May 2024
Anonim
C_68 C program to Compare two Strings | with strcmp() and without strcmp() function
Video: C_68 C program to Compare two Strings | with strcmp() and without strcmp() function

Content

String length comparison is a commonly used function in the C programming language, because it allows you to find out which string contains more characters. This is very useful for sorting data. String comparison requires a special function; so don't use: != or ==.

To step

  1. There are two functions that allow you to compare strings in C. Both are included in the string.h>code library.
    • strcmp () - This function compares two strings and returns the difference in the number of characters.
    • strncmp () - This also applies to strcmp (), except that this one is the first n compares characters. It is considered more secure as it helps prevent overflow crashes.
  2. Run the program with the necessary libraries. You have both libraries stdio.h> and string.h> needed, along with other libraries you may need for your program.

    #include stdio.h> [[Image: Compare Two Strings in C Programming Step 1 Version 4.webp | center]] #include string.h>

  3. Start one.intfunction. This is the easiest way to learn this function as it returns an integer comparing the length of the two strings.

    [[Image: Compare Two Strings in C Programming Step 2 Version 4.webp | center]] #include stdio.h> [[Image: Compare Two Strings in C Programming Step 3 Version 3.webp | center]] #include string. h> int main () {}

  4. Decide which two strings you want to compare. In this example we compare two data char strings. You must also determine the return value as an integer.

    [[Image: Compare Two Strings in C Programming Step 4 Version 4.webp | center]] #include stdio.h> [[Image: Compare Two Strings in C Programming Step 5 Version 4.webp | center]] #include string. h> int main () {char * str1 = "apple"; char * str2 = "orange"; int ret;}

  5. Add the compare function. Now that you have defined two strings, you can add the compare function. We go strncmp () so we need to make sure that the number of characters to measure is set within the function.

    [[Image: Compare Two Strings in C Programming Step 6 Version 4.webp | center]] #include stdio.h> #include string.h> int main () {char * str1 = "apple"; char * str2 = "orange"; int ret; ret = strncmp (str1, str2, 11); / * Compares the two strings up to 11 characters long * /}

  6. Use.If ... Elseto perform the comparison. Now that we've created the function, you use If ... Else to determine which string is longer. strncmp () gives 0 as a result, if the strings are the same length, a positive number if str1 is longer and a negative number if str2 is longer.

    #include stdio.h> #include string.h> int main () {char * str1 = "apple"; char * str2 = "orange"; int ret; ret = strncmp (str1, str2, 11); if (ret> 0) {printf ("str1 is longer"); } else if (ret 0) {printf ("str2 is longer"); } else {printf ("The two strings are the same"); } return (0); }

Warnings

  • Remember that the return value is 0 if the strings are equal. This can be confusing because 0 is also the value of FALSE.