天天看点

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字符串倒序函数时,同样记得空间分配问题。

继续阅读