views
There are two functions that allow you to compare strings in C. Both of these functions are included in the
Start the program with your necessary libraries. You'll want both the
Start an .int function. This is the easiest way to learn this function, as it will return an integer that compares the length of the two strings.
#include
Define the two strings you want to compare. For this example, we will be comparing two predefined char strings. You will also want to define the return value as an integer.
#include
Add the comparison function. 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
Use an .If...Else statement to perform the comparison. 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
Comments
0 comment