For example, you want to compare s1 == s2(0, 10), which means the first ten characters of string s2, use substr() may work it through, like this :

s1 == s2.substr(0, 10);

in which, s2.substr(pos, n) mean the continuous n characters begin with the pos-th position of string s2.

Another funtion is also goot to use :

s1.compare(pos1, n1, s2);

menas: compare the n1 characters begin from pos1 position of string s1, to string 2. You can checkout this funtion and its overloaded other five funtions.

Answer from smh on Stack Overflow
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › string_h › strncmp.php
C Language: strncmp function (Bounded String Compare)
In the C Programming Language, the strncmp function returns a negative, zero, or positive integer depending on whether the first n characters of the object pointed to by s1 are less than, equal to, or greater than the first n characters of the object pointed to by s2.
🌐
freeCodeCamp
freecodecamp.org › news › strcmp-in-c-how-to-compare-strings-in-c
strcmp in C – How to Compare Strings in C
April 27, 2023 - The strcmp() function is part of the standard C library ( string.h ). Its primary purpose is to compare the characters of the two strings in sequence until it finds a mismatch or until the end of the strings is reached (that is, the null character ...
Top answer
1 of 2
1

For example, you want to compare s1 == s2(0, 10), which means the first ten characters of string s2, use substr() may work it through, like this :

s1 == s2.substr(0, 10);

in which, s2.substr(pos, n) mean the continuous n characters begin with the pos-th position of string s2.

Another funtion is also goot to use :

s1.compare(pos1, n1, s2);

menas: compare the n1 characters begin from pos1 position of string s1, to string 2. You can checkout this funtion and its overloaded other five funtions.

2 of 2
1

To do this in-place, just use std::strncmp(). For example:

bool first_n_equal(const char *lhs, const char *rhs, size_t n) {
    return (std::strncmp(lhs, rhs, n) == 0);
}

Note that strncmp returns 0 if the two strings are equal, and will only compare up to n characters. To pick n you can hardcode 6, or loop over your string and check for where the first word ends. For example,

size_t size_of_first_word(const char *str) {
    size_t i;
    for (i = 0; str[i] != ' ' && str[i] != '\0'; i++) {}
    return i;
}

This function loops over the string until it hits a space or a null terminator (end of string). Then, to actually check your string for a command:

size_t input_len = size_of_first_word(input_string);
size_t command_len = size_of_first_word(command_string);
size_t check_len = std::min(input_len, command_len);
bool is_same = first_n_equal(input_string, command_string, check_len);

I intentionally made this verbose to make it easier to understand, so you can absolutely make this code smaller. You could also use std::string but it's not actually necessary.

🌐
BeginnersBook
beginnersbook.com › 2017 › 11 › c-strncmp-function
C strncmp() Function with example
This function compares only the first n (specified number of) characters of strings and returns following value based on the comparison.
🌐
Cplusplus
cplusplus.com › reference › string › string › compare
std::string::compare
Same as pos and len above, but for the comparing string. ... Pointer to an array of characters. If argument n is specified (4), the first n characters in the array are used as the comparing string.
🌐
Thinkage
thinkage.ca › gcos › expl › c › lib › strncm.html
STRNCMP - compare parts of two strings.
"strncmp" compares the first N characters of the string "s1" to the first N characters of the string "s2". If one or both of the strings is shorter than N characters (i.e. if "strncmp" encounters a '\0'), comparisons will stop at that point. Thus N represents the maximum number of characters ...
🌐
Cplusplus
cplusplus.com › reference › cstring › strncmp
Cplusplus
Compares up to num characters of the C string str1 to those of the C string str2. This function starts comparing the first character of each string.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 102046-how-do-i-compare-first-letters-two-strings.html
how do i compare first letters in two strings?
Bonus hint: the first letter of name1 is name1[0] and the first letter of name2 is name2[0]. ... it does now compile. It says "passins arg 2 of strcmp makes pointer from integer without a cast" Im using pointers to a string (i have to). ... If you ever need to compare a fixed-length of characters in a string, you can use strncmp(); Its the same as strcmp but it takes a third argument, which is the number of characters to compare.
🌐
CodinGame
codingame.com › playgrounds › 14213 › how-to-play-with-strings-in-c › string-length-string-comparison
How to play with strings in C
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.
Find elsewhere
🌐
IBM
ibm.com › docs › en › zos › 2.4.0
strncmp() — Compare strings
We cannot provide a description for this page right now
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string comparison in c
String Comparison in C: Methods & Examples
May 5, 2025 - At its core, string comparison in C means checking whether two strings are the same or determining which one comes first in a lexicographical (dictionary) order. This is useful in countless situations, like comparing passwords, sorting names, or validating input. But here's the thing: C doesn't treat strings as a built-in data type. Instead, strings in C are just arrays of characters ending with a null character (`'\0'`).
🌐
Programiz
programiz.com › c-programming › library-function › string.h › strcmp
C strcmp() - C Standard Library
#include <stdio.h> #include <string.h> int main() { char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; int result; // comparing strings str1 and str2 result = strcmp(str1, str2); printf("strcmp(str1, str2) = %d\n", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %d\n", result); return 0; } ... Your builder path starts here. Builders don't just know how to code, they create solutions that matter.
🌐
Linux Hint
linuxhint.com › comparing_strings_c_language
String Comparison: C Programming – Linux Hint
... Return value: The function returns 0 if the first n characters of the two strings are equal; otherwise, it returns negative or positive integer depending on the sign of the differences between the first mismatched character’s ASCII value. This function is similar to the function strcmp(), ...
🌐
Reactgo
reactgo.com › home › how to get the first n characters of a string in c
How to get the first n characters of a string in C | Reactgo
August 5, 2023 - To get the first n characters of a string, we can use the memcopy() function in C.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › stdstringcompare-in-c
std::string::compare() in C++ - GeeksforGeeks
May 6, 2025 - The string::compare() method also supports the comparison between a part of the string with another string. ... Returns 0 if substring is equal to str2. Returns a positive integer when the first not-matching character in substring has a greater ...
🌐
wikiHow
wikihow.tech › computers and electronics › software › programming › c programming languages › how to compare two strings in c programming: 10 steps
How to Compare Two Strings in C Programming: 10 Steps
June 16, 2025 - There are two functions that allow ... 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....
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › strcmp in c
strcmp in C | String Comparison Function with Examples
February 5, 2025 - The general syntax of the strcmp() function is as follows: ... *const char str1: This is the first string you want to compare. It’s a constant character pointer (const char *), meaning it points to the string's first character.
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › characters and strings
strncmp - Compare first n characters of strings (case sensitive) - MATLAB
Create two different character vectors. Compare the first 11 characters of them. s1 = 'Kansas City, KS'; s2 = 'Kansas City, MO'; tf = strncmp(s1,s2,11) ... Compare the two character vectors using strcmp. ... Create a string array that contains names.
🌐
Physics Forums
physicsforums.com › other sciences › programming and computer science
How to search just the first two characters in a c-string? • Physics Forums
November 7, 2020 - So I want to search only the first character or the first two characters for matching only. There lies the problem, It's easy to do string compare strcmp( string1, string2) =0 to find match.