How to Compare Two Strings in C Programming

There are two functions that allow you to compare strings in C. Both of these functions are included in the library., Start the program with your necessary libraries., Start an .int function., Define the two strings you want to compare., Add the...

6 Steps 2 min read Medium

Step-by-Step Guide

  1. Step 1: There are two functions that allow you to compare strings in C. Both of these functions are included in the <string.h> library.

    strcmp()
    - This function compares two strings and returns the comparative difference in the number of characters. strncmp()
    - This is the same as strcmp(), except that it compares the first n characters.

    It is considered more secure as it helps prevent crashes from overflow.
  2. Step 2: Start the program with your necessary libraries.

    You'll want both the <stdio.h> and <string.h> libraries, along with any others you may need for your specific program. #include <stdio.h>] #include <string.h>

    This is the easiest way to learn this function, as it will return an integer that compares the length of the two strings. ] #include <stdio.h>] #include <string.h> int main () { } , For this example, we will be comparing two predefined char strings.

    You will also want to define the return value as an integer.] #include <stdio.h>] #include <string.h> int main () { char *str1 = "apple"; char *str2 = "orange"; int ret; } , Now that you have your two strings defined, you can add the comparison function.

    We are going to use strncmp(), so we need to ensure that the number of characters to be measured is set in the function. ] #include <stdio.h> #include <string.h> int main () { char *str1 = "apple"; char *str2 = "orange"; int ret; ret = strncmp(str1, str2, 6); /*This will compare the two strings up to 6 characters long */ } , Now that you have the function in place, you can use an If...Else statement to display which string is longer. strncmp() will return 0 if the strings are the same length, a positive number if str1 is larger, and a negative number if str2 is larger. #include <stdio.h> #include <string.h> int main () { char *str1 = "apple"; char *str2 = "orange"; int ret; ret = strncmp(str1, str2, 6); if(ret > 0) { printf("str1 is longer"); } else if(ret < 0) { printf("str2 is longer"); } else { printf("The two strings are equal"); } return(0); }
  3. Step 3: Start an .int function.

  4. Step 4: Define the two strings you want to compare.

  5. Step 5: Add the comparison function.

  6. Step 6: Use an .If...Else statement to perform the comparison.

Detailed Guide

strcmp()
- This function compares two strings and returns the comparative difference in the number of characters. strncmp()
- This is the same as strcmp(), except that it compares the first n characters.

It is considered more secure as it helps prevent crashes from overflow.

You'll want both the <stdio.h> and <string.h> libraries, along with any others you may need for your specific program. #include <stdio.h>] #include <string.h>

This is the easiest way to learn this function, as it will return an integer that compares the length of the two strings. ] #include <stdio.h>] #include <string.h> int main () { } , For this example, we will be comparing two predefined char strings.

You will also want to define the return value as an integer.] #include <stdio.h>] #include <string.h> int main () { char *str1 = "apple"; char *str2 = "orange"; int ret; } , Now that you have your two strings defined, you can add the comparison function.

We are going to use strncmp(), so we need to ensure that the number of characters to be measured is set in the function. ] #include <stdio.h> #include <string.h> int main () { char *str1 = "apple"; char *str2 = "orange"; int ret; ret = strncmp(str1, str2, 6); /*This will compare the two strings up to 6 characters long */ } , Now that you have the function in place, you can use an If...Else statement to display which string is longer. strncmp() will return 0 if the strings are the same length, a positive number if str1 is larger, and a negative number if str2 is larger. #include <stdio.h> #include <string.h> int main () { char *str1 = "apple"; char *str2 = "orange"; int ret; ret = strncmp(str1, str2, 6); if(ret > 0) { printf("str1 is longer"); } else if(ret < 0) { printf("str2 is longer"); } else { printf("The two strings are equal"); } return(0); }

About the Author

A

Ann Ford

Brings years of experience writing about pet care and related subjects.

86 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: