Download Article Download Article

Comparing string length is a common function in C programming, as it allows you to see which string contains more characters. This is very useful for sorting data. Comparing strings requires a special function; do not use != or ==.

  1. 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. 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>
    
    Advertisement
  3. 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 <stdio.h>
    #include <string.h>
    
    int main ()
    
    {
    
    }
    
  4. For this example, we will be comparing two predefined char strings. You will also want to define the return value as an integer.[1]
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char *str1 = "apple";
      char *str2 = "orange";
      int ret;
    }
    
  5. 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 */
    }
    
  6. 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 <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);
    }
    
  7. Advertisement

Community Q&A

Search
Add New Question
  • Question
    What is the different between strcmp and strncmp?
    Living Concrete
    Living Concrete
    Top Answerer
    Strcmp() simply compares two strings; strncmp() compares the first n characters of two string together.
  • Question
    Can I use = or != when comparing two strings?
    WalkerTyme
    WalkerTyme
    Community Answer
    You can use == or != if you need to check if the strings are exactly the same.
  • Question
    When i read the line ret = strcomp (str1, str, 6), I don't understand it.
    Community Answer
    Community Answer
    The function 'strncomp' returns three values: 0 if the strings are equal, >0 if the first string is longer.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Tips

Submit a Tip
All tip submissions are carefully reviewed before being published
Thanks for submitting a tip for review!

Warnings

  • Remember that the return value is 0 if the strings are the same. This could confuse you because 0 is also the value of FALSE.
Advertisement

You Might Also Like

Become a ProgrammerBecome a Programmer
Learn a Programming LanguageLearn a Programming Language
Create a Program in C SharpCreate a Program in C Sharp
Compile a C Program Using the GNU Compiler (GCC)How to Use GCC to Compile a C Program on Linux and Windows
Edit DLL Files in Visual StudioEdit DLL Files in Visual Studio
Delay in CDelay in C
Set Up SDL with Visual StudioSet Up SDL with Visual Studio
Run CUDA C or C++ on Jupyter (Google Colab)Run CUDA C or C++ on Jupyter (Google Colab)
Get Color in C ProgramGet Color in C Program
Print in C2 Easy Ways to Print in C and C++ Programming
Set Up OpenGL GLFW GLEW GLM on a Project with Visual StudioSet Up OpenGL GLFW GLEW GLM on a Project with Visual Studio
Set Up OpenGL‐GLFW‐GLAD on a Project with Visual StudioSet Up OpenGL‐GLFW‐GLAD on a Project with Visual Studio
Start Learning C Programming in Turbo C++ IDEStart Learning C Programming in Turbo C++ IDE
Check Null in CCheck Null in C
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 25 people, some anonymous, worked to edit and improve it over time. This article has been viewed 520,067 times.
How helpful is this?
Co-authors: 25
Updated: August 7, 2019
Views: 520,067
Article SummaryX

1. Start an int function.
2. Define the strings you want to compare.
3. Define the return value as an integer.
4. Add the strncmp() function.
5. Add an If...Else statement to compare.

Did this summary help you?

Thanks to all authors for creating a page that has been read 520,067 times.

Is this article up to date?

Advertisement