这是字符数组,不是字符串
这是字符串,在最后加了'\0'
字符串变量可以有下面几种表示方式
char *str = "HELLO";
char word[] = "HELLO";
char line[10] = "HELLO"; //占了6个字节,因为还有末尾的/0
#include <stdio.h>
int main(){
char *s = "Hello World";
s[0] = 'B';
printf("Here!s[0] = %c\n",s[0]); 会报错
return 0;
}
char *s = "Hello World";
char *s2 = "Hello World";
//s[0] = 'B';
printf("s = %p\n",s);
printf("s2 = %p\n",s2);
printf("Here!s[0] = %c\n",s[0]);
return 0;
s和s2的值相同,都指向计算机中的同一段代码段,且只可读不可写
如果需要修改字符串,应该用数组来定义字符串:
char s[] = "Hello,World";
char s3[] = "HelloWorld";
s3[0] = 'B';
printf("Here!s3[0] = %c\n",s3[0]);
结果为Here!s3[0] = B
修改成功
字符串可以表达为char *的形式,但char *不一定是字符串
只有它所指的字符数组结尾有0,才能说它所指的是字符串
char *t = "title";
char *s;
s = t;
实现的是s指向了t指向的代码段,并没有重新造一个s出来
字符串的输入输出
char string[8];
scanf("%s",string); //只读到空格tab或者回车为止,空格和tab后面的字符串需要再一次%s进行读入
printf("%s",string);
char word[8];
char word2[8];
scanf("%s",word);
scanf("%s",word2);
printf("%s##%s##\n",word,word2);
输入hello world //相当于hello是word,world是word2
读到hello##world##
scanf只读入一个单词
scanf("%7s",word);表示告诉scanf最多可以读7个字符,超过7个字符就不要了
char word[8];
char word2[8];
scanf("%7s",word); 规定scanf一次只能读7个字符
scanf("%7s",word2);
printf("%s##%s##\n",word,word2);
输入123 12345678
结果为 123##1234567##
输入12345678
结果为1234567##8## 第一个字符串超过了7个字符,第8个字符变成了第二个scanf
字符串数组
char **a 意思是a是一个指针,指向另一个指针,那个指针指向一个字符(串)
char a[][]
int main(int argc,char const *argv[]){
int i;
for(i = 0;i < argc;i++){
printf("%d:%s\n",i,argv[i]);
}
}
执行./a.out
输出0:./a.out
再输入./a.out 123
输出0:./a.out
1:123
再输入./a.out 123 456 wrd rds
输出0:./a.out
1:123
2:456
3:wrd
4:rds
单个字符的输入输出
putchar
int putchar(int c);
向标准输出写一个字符,返回写了几个字符,EOF(-1)表示写失败
getchar
int getchar(void);
从标准输入读入一个字符,返回类型是int是为了返回EOF(-1)
#include <stdio.h>
int main(int argc,cahr const *argv[]){
int ch;
while((ch = getchar()) != EOF){
putchar(ch); 如果不是EOF就输出ch
}
printf("EOF\n");
return 0;
}
只有ctrl+z可以停止程序
字符串函数#include <string.h>
1、
size_t strlen(const char *s); //返回s的字符串长度
#include <stdio.h>
#include <string.h>
size_t mylen(const char* s){ //代替strlen
int index; //索引
while(s[index] != '\0'){ //重要点
index++;
}
return index;
}
int main(int argc,cahr const *argv[]){
char line[] = "Hello";
//printf("strlen = %lu\n",strlen(line)); //5
printf("strlen = %lu\n",mylen(line)); //5
printf("sizeof = %lu\n",sizeof(line)); //6
return 0;
}
sizeof算上了\0
2、函数strcmp
int strcmp(const char* s1,const char *s2); //比较两个字符串
返回值为0,则s1 == s2;返回1,则s1 > s2;返回-1,则s1 < s2;
#include <stdio.h>
#include <string.h>
int mycmp(const char* s1,const char* s2){
// int index = 0;
// while(s1[index] == s2[index] && s1[index] != 0){
// //if(s1[index] != s2[index]){ //把if的条件都写道while条件里面去
// // break;
// //}else if(s1[index] == '\0'){
// // break;
// //}
// index++;
// }
// return s1[index] - s2[index];
while(*s1 == *s2 && *s1 != 0){
s1++;
s2++;
}
return *s1 - *s2;
}
int main(int argc,char const *argv[]){
char s1[] = "abc";
char s2[] = "abc";
printf("%d\n",strcmp(s1,s2)); //结果为0
return 0;
}
char s1[] = "abc";
char s2[] = "bbc";
printf("%d\n",strcmp(s1,s2)); //结果为-1,s1比s2小
3、函数strcpy
char *strcpy(char *restrict dst,const char *restrict src);
把src的字符串拷贝到dst,restrict表明src和dst不重叠
返回dst
复制一个字符串
char *dst = (char*)malloc(strlen(src)+1);
strcpy(dst,src);
#include <stdio.h>
#include <string.h>
char *mycpy(char *dst,const char *src){
//数组版本
int idx = 0;
while(src[idx] != '\0'){
dst[idx] = src[idx];
idx++;
}
dst[idx] = '\0';
return dst;
//指针版本
char *ret = dst; //由于下面有dst++,所以要先保存dst的首地址
while(*src != '\0'){
*dst = *src;
dst++;
src++;
}
*dst = '\0';
return ret;
}
4、字符串搜索函数
char *strchr(const char *s,int c);
char *strrchr(const char *s,int c);
返回NULL表示没有找到
int main(int argc,char const *argv[]){
char s[] = "hello";
char *p = strchr(s,'l');
char *t = (char*)malloc(strlen(p)+1); //把llo复制到另一个字符串中
strcpy(t,p);
printf("%s\n",p); //输出结果为llo
free(t);
}