|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 BaysideLizard 于 2023-11-14 22:25 编辑
- #include <stdio.h>
- #include <string.h>
- //DNX写于2023年9月26日
- int main(void)
- {
- char zzh1[] = "abcd";
- char zzh2[] = "efgh";
- char sum[100];
-
- strcpy (zzh1,zzh2);//复制字符串函数
- printf ("strlen str = %u\n\n",strlen(zzh1));//获取字符串长度函数
- printf("zzh1 = %s\n",zzh1);
- printf("zzh2 = %s\n\n",zzh2);
- strcpy(zzh1,"abcd");
- strncpy(zzh1,"xyzh",4);//strncoy函数不会追加\0
- zzh1[4] = '\0';//需要手动追加\0
- strcat(zzh1,zzh2);//连接字符串函数
- printf("zzh1 = %s \n\n",zzh1);
- strcpy(zzh1,"abcd");
- printf("zzh1 = %s \n",zzh1);
- printf("zzh2 = %s \n",zzh2);
- if (strcmp(zzh1,zzh2) == 0)//比较字符串函数,一致返回0,存在差异返回非0
- {
- printf("两个字符串完全一致!\n");
- }
- else
- {
- printf("两个字符串有差异!\n");
- }
-
- return 0;
- }
- /*3.2字符串处理函数
- 获取字符串长度函数:strlen(),返回unsigned int %u
- 复制字符串函数:strcpy(),strncpy()
- 连接字符串函数:strcat(),strncat()
- 比较字符串函数:strcmp()
- */
复制代码
运行结果:
strlen str = 4
zzh1 = efgh
zzh2 = efgh
zzh1 = xyzhefgh
zzh1 = abcd
zzh2 = efgh
两个字符串有差异!
--------------------------------
Process exited after 0.03388 seconds with return value 0
请按任意键继续. . .
在FishC学C的第六天
加油! |
|