重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
没有这样的库函数,都比较简单的,自己写个好了如果是字符串数组,strchramp;lt;string.hamp;gt;可以查找某个字符是否在这个字符串数组中
富蕴ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
#include stdio.h
/*比较两个字符串*/
/*src、dst相等 return 1*/
/*src比dst短 return -2*/
/*src比dst长 return 2*/
/*src、dst长度一致但不相等 return -1*/
int stringCmp (char *src, char *dst) {
if (strlen (src) strlen (dst))
return -2;
else if (strlen (src) strlen (dst))
return 2;
else {
while (*src *dst) {
if (*src != *dst)
return -1;
src++;
dst++;
}
return 1;
}
}
/*合并两个字符串*/
void mergeString(char* str1, char* str2, char* merge) {
while (*merge++ = *str1++); /*复制str1并将指针移动到字符串的末尾的下一个位置*/
merge--; /*退到字符串的末尾*/
while (*merge++ = *str2++); /*从末尾(\0)的位置开始将str2拷贝进来*/
*merge = '\0';
}
int main(void) {
char *str1_1="hel",*str1_2="lo", *merge1;
char *str2_1="ness",*str2_2="happy", *merge2;
char *str3_1="hi",*str3_2="hi", *merge3;
char *str4_1="hello",*str4_2="world", *merge4;
puts ("第1组字符串:");
puts (str1_1);
puts (str1_2);
if (stringCmp (str1_1, str1_2) == 1)
puts (str1_1);
else if (stringCmp (str1_1, str1_2) == -2) {
mergeString (str1_2, str1_1, merge1);
puts (merge1);
}
else if (stringCmp (str1_1, str1_2) == 2 || stringCmp (str1_1, str1_2) == -1) {
mergeString (str1_1, str1_2, merge1);
puts (merge1);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第2组字符串:");
puts (str2_1);
puts (str2_2);
if (stringCmp (str2_1, str2_2) == 1)
puts (str2_1);
else if (stringCmp (str2_1, str2_2) == -2) {
mergeString (str2_2, str2_1, merge2);
puts (merge2);
}
else if (stringCmp (str2_1, str2_2) == 2 || stringCmp (str2_1, str2_2) == -1) {
mergeString (str2_1, str2_2, merge2);
puts (merge2);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第3组字符串:");
puts (str3_1);
puts (str3_2);
if (stringCmp (str3_1, str3_2) == 1)
puts (str3_1);
else if (stringCmp (str3_1, str3_2) == -2) {
mergeString (str3_2, str3_1, merge3);
puts (merge3);
}
else if (stringCmp (str3_1, str3_2) == 2 || stringCmp (str3_1, str3_2) == -1) {
mergeString (str3_1, str3_2, merge3);
puts (merge3);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
puts ("第4组字符串:");
puts (str4_1);
puts (str4_2);
if (stringCmp (str4_1, str4_2) == 1)
puts (str4_1);
else if (stringCmp (str4_1, str4_2) == -2) {
mergeString (str4_2, str4_1, merge4);
puts (merge4);
}
else if (stringCmp (str4_1, str4_2) == 2 || stringCmp (str4_1, str4_2) == -1) {
mergeString (str4_1, str4_2, merge4);
puts (merge4);
}
putchar ('\n');
puts ("======================================================");
putchar ('\n');
getch (); /*屏幕暂留*/
return 0;
}
运行结果
#include
"stdio.h"
void
input(int
a[10])
//给一维数组输入数据
{
int
i;
for(i=0;i10;i++)
scanf("%d",a[i]);
}
void
output(int
a[10])
//输出一维数组输入数据
{
int
i;
for(i=0;i10;i++)
printf("%d
",a[i]);
}
int
main()
{
int
b[10];
input(b);
//调用input函数给数组b赋值
output(b);//调用output函数输出数组b中的值
}
没有这样的函数,因为C语言面向的是程序员,所有程序内部逻辑要程序员自己控制,数组是否越界的判断要交给程序员自己做。
如果是字符串,可以使用strlen函数计算字符串长度,不包含最后的结束符。