The little world of strings

 First, the appearance of the string
char arr[10]={'a','b','c'};//The remainder is '/0', which is a string
 char brr[]={'a','b','c' };//There is no '\0', it is a character array, not a string
 char crr[10]={"abc"};//Character array is unique, it is a string
 char drr[10]="abc";// Character array is unique, it is a string
 char err[]="abc";//Character array is unique, it is a string
 char frr[10]="";//There are 10 '\0' in the string, which is a string
 In a word, the character sequence included in the string with " ", with '\0', and the end marker of the string is '\0', as long as there is '\0', it is a string.
Second, the operation of strings

(1) String copy
#include <stdio.h>

void  Mystrcpy(char *des, char *str) 
{
  int i;
  for(i = 0;str[i] != '\0';i++)
  {
   des[i] = str[i];
  }
   des[i] = '\0';
   printf("%s\n",des);
}
int main()
{
 char str[]={"asdf"};
 char des[10];
 Mystrcpy(des,str);
}
(2) String connection
void  Mystrcat(char des[],char str[])
{  
 int i,j;
 i=0;
 j=0;
 while(des[i]!='\0')
 { i++;   }
 while(str[j]!='\0')
 { des[i++]=str[j++];}
 des[i]='\0';
 
}
int main()
{
 char des[]={"absdf"};
 char str[]={"eyreu"};
    Mystrcat(des,str);
    printf("%s\n",des);
 return 0;
}
(3) Comparison of strings
#include <stdio.h>
#include <string.h>
int Mystrcmp(char *des,char *src)
{
 int i=0;
 while(*(des+i) == *(src+i))
   if(*(des+i++) < '\0')
    return 0;
       return (*(des+i)-*(src+i)); 
}
int main()
{
 char str1[10] = {"abcde"} ;
 char str2[10] = {"abcd"} ;
 char str3[10] = {"abc"} ;
 char str4[10] = {"abc"} ;
 printf("%d\n",Mystrcmp(str1,str2));
 printf("%d\n",Mystrcmp(str3,str4));
 return 0;
}

Three, the two sides of the string

                  char *str1 = "abcde";//String constant, modification is not allowed

c har str2[] ​​= "abcde";//Character array, there is \0, so it is also a string

str1[0] = 'x';//Attempt to modify the value of the string constant, write error
str2[0] = 'x';

strcpy(str1,"xyz");    //error

             Copy str1 into "xyz", but str1 is a string constant and cannot be modified

strcpy(str2,"hello world");//error out of bounds, because str2 only occupies 6 bytes

Solution: String crash reasons:

           (1) Attempt to modify the value of a string constant

           (2) Out of bounds

 int main()
 {
char str1[10];
gets(str1);//Dangerous, out of bounds, it is recommended to use fgets()
printf("str1=%s\n",str1);

return 0;

 }

Additional:
Difference between string sizeof and  strlen

intmain()

{
char str1[100] = "abcde";//100,5 specifies the size of the array, so the occupied bytes are 100, and the length of the string is the valid length does not include '\0', so the length is 5

           char str2[] = "abcde";//6,5 没有指定大小,所占字节为6长度为5

      char str3[100] = "abcdef\0ijk\n";//100,6  指定了数组大小,所以所占字节为100,求字符串长度时,遇'\0'结束,所以长度为6

          char str4[] = "abcdef\0ijk\n";//12,6 算所占字节大小时,不以'\0'为结束标志,切勿忘记字符串自带的'\0',所以字节大小为12 

char *str5 = "abcde";//4,5    定义了一个指向字符串的指针,指针所占字节为4 

char *str6 = "abcdef\0ijk\n";//4,6  同上

 printf("%d,%d\n",sizeof(str1),strlen(str1));
printf("%d,%d\n",sizeof(str2),strlen(str2));
printf("%d,%d\n",sizeof(str3),strlen(str3));
printf("%d,%d\n",sizeof(str4),strlen(str4));
printf("%d,%d\n",sizeof(str5),strlen(str5));
printf("%d,%d\n",sizeof(str6),strlen(str6));

return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325651961&siteId=291194637