C语言当中字符串是以空字符 \0 结尾的字符数组。
cs
#include <string.h>
cs
char str[] = "Hello" //字符类型的数组
实际当中的内存存储形式 'H' 'e' 'l' 'l' 'o' '\0';数组长度是6
cs
char *s = "Hello"; // 指针类型的数组 字符串常量(值不能改变的量) str1[0]='h'不能这么写
如何去定义字符串
cs
char s1[] = "Hello" //推荐 长度是6 \0
char s2[20] = "Hello"; //开辟大小是20的内存空间,前6位是'H' 'e' 'l' 'l' 'o' '\0' 后边是随机数
char s3 = { 'H', 'e' ,'l' ,'l' ,'o','\0' }; //不推荐
字符串如何输出
cs
printf("%s \n",s1);
//输出单个字符
printf("%c" \n",s1[1]);
返回整个字符串的长度
cs
#include <string.h>
int len=strlen(str); //输出时不包含'\n'
指针遍历
cs
int main(){
char s[] ="Hello";
int len=strlen(s);
printf("%d \n",len);
char *p=s;
while(*p!='\0'){
printf("%c \n",*p);
p++;
}
}
-----------------------------------------------------算法题------------------------------------------------------------------
逆转输出 / KMP算法
cs
//Hello 逆转输出
void re(char s[]){
int len = strlen(s);
char *p=s;
for(int i=len;i>=0;i--){
printf("%c",s[i]);
}
}
树:


回溯问题的模版:
