برنامه ای بنویسید که با استفاده از تابع strncmp دو رشته را که کاربر وارد می کند را با هم مقایسه کند. این برنامه باید تعداد کاراکترهایی را که باید با هم مقایسه شوند را از کاربر گرفته و اعلام کند که ایا رشته اول کوچک تر مساوی و یا بزرگ تر از رشته ی دوم است.
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
#include<cstring>
const int SIZE = 20;
int main()
{
char string1[SIZE], string2[SIZE];
int result, compareCount;
cout << "Enter two strings: ";
cin >> string1 >> string2;
cout << "How many characters should be compared: ";
cin >> compareCount;
result = strncmp(string1, string2, compareCount);
if(result > 0)
cout << '\"' << string1 << "\" is greater than\"" << string2
<< "\" up to " << compareCount << " characters\n";
else if (result == 0)
cout << '\"' << string1 << "\" is equal to \"" << string2
<< "\" up to " << compareCount << " characters\n";
else
cout << '\"' << string1 << "\" is less than \"" << string2
<< "\" up to " << compareCount << " characters\n";
cout << endl;
return 0;
}