string.h

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      

void teststr();
void testmem();

int main()
{
    testmem();
    //teststr();
    return 0;
}

void testmem()
{
    char str[12] = "hello world";
    char str1[12];
    memset(str,'a',5);//不可以为str = "hello world";
    printf("%s\n",str);
    memcpy(str1,str,12);
    printf("%s\n",str1);
    //memmove()可以复制重叠区域
}

void teststr()
{
    char *str1,str2[6],str3[12],*str4;
    int comp;

    str2[0] = 'a';
    str2[1] = 'b';
    str2[2] = 'c';
    str2[3] = 'd';
    str2[4] = 'e';
    str2[5] = 0;
    printf("%s\n",str2);
    str1 = "hello";
    strcpy(str2,str1);
    printf("%s\n",str2);

    str1 = " world";
    strcpy(str3,str2);
    strcat(str3,str1);
    printf("%s\n",str3);

    str1 = strchr(str3,'w');//strrchr()
    printf("%s\n",str1);
    str1 = strstr(str3,"wo");
    printf("%s\n",str1);

    comp = strcmp(str2,str1);//stricmp()
    printf("%s %s %d\n",str2,str1,comp);
    str1 = "aab";
    str4 = "aac";
    comp = strncmp(str1,str4,2);//strnicmp()
    printf("%d\n",comp);

    str1 = strrev(str3);//如果str3不是数组形式的,程序将崩溃
    printf("%s\n",str1);
    strset(str1,'a');
    printf("%s\n",str1);
    strnset(str1,'b',4);
    printf("%s\n",str1);
    str1 = strupr(str1);
    printf("%s\n",str1);
    str1 = strlwr(str1);
    printf("%s\n",str1);
}

     
     
    
    
   
   


注意

1、在进行字符串操作时,分配空间的时候要至少多分配一个空间,用来存放'\0'。

2、在使用memset memcpy memmove函数时,如果使用指针不要忘记为其分配空间。

3、在使用strrev字符串倒序函数时,同样记得空间分配问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值