📅  最后修改于: 2020-09-25 09:12:40             🧑  作者: Mango
int strncmp( const char* lhs, const char* rhs, size_t count );
strncmp()
函数采用两个参数: lhs
, rhs
和count
。它按字典顺序比较lhs
和rhs
的内容,最多可计数字符。结果的符号是lhs
和rhs
不同的第一对字符之间的差异的符号。
如果lhs或rhs都不指向以null结尾的字符串,则strncmp()
的行为是不确定的。
它在
strncmp()
函数返回:
#include
#include
using namespace std;
void display(char *lhs, char *rhs, int result, int count)
{
if(result > 0)
cout << rhs << " precedes " << lhs << endl;
else if (result < 0)
cout << lhs << " precedes " << rhs << endl;
else
cout << "First " << count << " characters of " << lhs << " and " << rhs << " are same" << endl;
}
int main()
{
char lhs[] = "Armstrong";
char rhs[] = "Army";
int result;
result = strncmp(lhs,rhs,3);
display(lhs,rhs,result,3);
result = strncmp(lhs,rhs,4);
display(lhs,rhs,result,4);
return 0;
}
运行该程序时,输出为:
First 3 characters of Armstrong and Army are same
Armstrong precedes Army