BaysideLizard 发表于 2023-10-5 11:01:09

S1E19:字符串处理函数

本帖最后由 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;
   
    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 = '\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的第六天
加油!{:5_95:}

六翻了 发表于 2023-10-5 13:33:07

加油
页: [1]
查看完整版本: S1E19:字符串处理函数