您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

写一个C程序,使用strncmp库函数来比较两个字符串

2024/3/3 8:53:30发布17次查看
strncmp是一个预定义的库函数,存在于string.h文件中,它用于比较两个字符串并显示哪个字符串更大。
strcmp函数(字符串比较)此函数比较两个字符串。它返回两个字符串中第一个不匹配字符的ascii差异。
语法int strcmp (string1, string2);
如果差异等于零,则 string1 = string2。
如果差异为正,则 string1> string2。
如果差异为负,则 string1 <string2。
示例
strncmp 函数此函数用于比较两个字符串的前 n 个字符。
语法strncmp ( string1, string2,2)
程序#include<stdio.h>#include<string.h>void main(){ //declaring two strings// char string1[25],string2[25]; int value; //reading string 1 and string 2// printf("enter string 1: "); gets(string1); printf("enter string 2: "); gets(string2); //comparing using library function// value = strncmp(string1,string2,4); //if conditions// if(value==0){ printf("%s is same as %s",string1,string2); } else if(value>0) { printf("%s is greater than %s",string1,string2); } else { printf("%s is less than %s",string1,string2); }}
输出enter string 1: tutorialsenter string 2: pointtutorials is greater than point
以上就是写一个c程序,使用strncmp库函数来比较两个字符串的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product