A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/stdstrncmp-in-c/ below:

std::strncmp() in C++ - GeeksforGeeks

std::strncmp() in C++

Last Updated : 02 Jul, 2024

std::strncmp() function in C++ lexicographically compares not more than count characters from the two null-terminated strings and returns an integer based on the outcome. This function is a Standard Library function that is defined in <cstring> header file in C++.

Syntax
int strncmp(const char *str1, const char *str2, size_t count);
Parameters Return Value

If there are fewer than count characters in either string, the comparison ends when the first null is encountered.

Example of strncmp() C++
// C++ program to illustrate the strncmp function
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
    // defining two similar strings
    char* str1 = "GeeksforGeeks";
    char* str2 = "Geeks For Geeks";

    // comparing first 5 characters of each
    int value = strncmp(str1, str2, 5);

    if (!value) {
        cout << "The first 5 characters are same.";
    }
    else {
        cout << "The first 5 characters are not equal.";
    }
  
    return 0;
}

Output
The first 5 characters are same.
What does strcmp() function return?

When the strings are not equal, the value returned by the strncmp() function is the difference between the ASCII values of the first unmatched character in str1 and str2.

The strncmp() function returns three different types of integer values on the basis of comparison.

1. Greater than zero ( >0 )

A positive value is returned if a character of str1 and str2 doesn't match before the num characters and the ASCII value of the str1 character is greater than the ASCII value of the str2 character. As a result, we can say that str1 is lexicographically greater than str2.

Example

C++
// C, C++ program to demonstrate
// functionality of strncmp()
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
    // Take any two strings
    char str1[10] = "aksh";
    char str2[10] = "akash";

    // Compare strings using strncmp()
    int result = strncmp(str1, str2, 4);

    if (result == 0) {
        // num is the 3rd parameter of strncmp() function
        cout << "str1 is equal to str2 up to num characters"
             << endl;
    }
    else if (result > 0)
        cout << "str1 is greater than str2" << endl;
    else
        cout << "str2 is greater than str1" << endl;

    cout << "Value returned by strncmp() is: " << result
         << endl;

    return 0;
}

Output
str1 is greater than str2
Value returned by strncmp() is: 18
2. Less than zero ( <0 )

A negative value is returned, if a character of str1 and str2 doesn't match before the num characters and the ASCII value of the str1 character is lesser than the ASCII value of the str2 character. As a result, we can say that str2 is lexicographically greater than str1.

Example

C++
// C, C++ program to demonstrate
// functionality of strncmp()

#include <cstring>
#include <iostream>
using namespace std;

int main()
{
    // Take any two strings
    char str1[10] = "akash";
    char str2[10] = "aksh";

    // Compare strings using strncmp()
    int result = strncmp(str1, str2, 4);

    if (result == 0) {
        // num is the 3rd parameter of strncmp() function
        cout << "str1 is equal to str2 up to num characters"
             << endl;
    }
    else if (result > 0)
        cout << "str1 is greater than str2" << endl;
    else
        cout << "str2 is greater than str1" << endl;

    cout << "Value returned by strncmp() is: " << result
         << endl;

    return 0;
}

Output
str2 is greater than str1
Value returned by strncmp() is: -18
3. Equal to zero ( 0 )

This function returns zero if the characters of str1 match with the characters of the str2 up to num characters. As a result, we cannot say that str1 is equal to str2 until num is equal to the length of either string.

Example

C++
// C, C++ program to demonstrate
// functionality of strncmp()

#include <cstring>
#include <iostream>
using namespace std;

int main()
{
    // Take any two strings
    char str1[10] = "akash";
    char str2[10] = "akas";

    // Compare strings using strncmp()
    int result = strncmp(str1, str2, 4);

    if (result == 0) {
        // num is the 3rd parameter of strncmp() function
        cout << "str1 is equal to str2 up to num characters"
             << endl;
    }
    else if (result > 0)
        cout << "str1 is greater than str2" << endl;
    else
        cout << "str2 is greater than str1" << endl;

    cout << "Value returned by strncmp() is: " << result
         << endl;

    return 0;
}

Output
str1 is equal to str2 up to num characters
Value returned by strncmp() is: 0
Important Points Related Articles

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4