深入浅出 C语言之字符串

C 字符串

  • 定义字符串
  • 声明字符大小要比字符串多1,存储\0
// 方式一
char name1[] = {'a', 'b', 'c', '\0'}
// 方式二
char name2[] = "abc"
// 方式三
char * name3 = "abc"
  • 字符串操作
// 录入
gets(name)  // gets不安全,没有边界
// fgets(变量名,长度,输入源)
fgets(name, 50, stdin)
// 输出
puts(name) // puts自带\n
  • 常用字符串函数
// 返回str 里\n的指针
char * find = strchr(str, '\n')
// 返回长度,中文占2个字节
char word[] = "hellowworld"
strlen(word)
// 字符串复制
strcpy(str1, str)
// 字符串相等
strcmp(str1,str2) == 0 //相等
// 字符串拼接
strcat(str1, str2) // str1的长度必须足够容纳str1+str2的长度
// 字符串加密
char password[50] = "12345"
const KEY = 5
int i = 0
for(; i< strlen(password); i++) 
{
    password[i] = password[i] + i + KEY
}

猜你喜欢

转载自blog.csdn.net/millions_02/article/details/79822270