C语言Char和string

字符与字符串

存储

char字符其实是数字,根据ascil码可查

65-90是A-Z,97-122是a-z

字符串一般以’\0’结尾

注意在赋值时,字符char c=’a’,字符串char * a=”abc”或者

char a[]=={‘a’,’b’,’c’};注意这样赋值字符串不会以’\0’结尾

char * 与char x[]

由于C语言中没有真正的字符串类型,可以通过字符数组表示字符串,因为它的元素地址是连续的,这就足够了。

C语言中规定数组代表数组所在内存位置的首地址,也是 str[0]的地址,即str = &str[0];

在C语言中字符串常量的本质表示其实是一个地址

%s的原理其实也是通过字符串首地址输出字符串,所以可以char *str; printf(“%s”,str);

char * 与 char a[ ] 的本质区别:

当定义 char a[10 ] 时,编译器会给数组分配十个单元,每个单元的数据类型为字符。。

而定义 char *s 时, 这是个指针变量,只占四个字节,32位,用来保存一个地址。。

写程序是尽量用char a[]; 来定义string,免得出锅

单个字符的输入与输出。

通常,我们用char定义的字符型变量可以用以下形式进行输入:

1
2
3
char ch;
ch = getchar();
scanf("%c",&ch);

同样的,输出的格式我们也有:

1
2
ch = putchar();
printf("%c",ch);

字符串的输入输出

scanf遇到空格、回车就会终止!而gets认为空格也是字符,可以将空格输入,遇到回车才会终止!

printf输出时,需要加\n才会换行;而puts函数自带换行功能!

输入

1
2
3
4
5
6
7
8
char a[10];
scanf("%s",a);//注意这里没有地址符!!!
//当输入的字符串中有空格就会停止
char* str;
str=(char*)malloc(sizeof(char)*10);
gets(str);
fgets(str, sizeof(str), stdin);
scanf("%[^\n]",a);

下面是 fgets() 函数的声明。

1
char *fgets(char *str, int n, FILE *stream)

参数

  • str – 这是指向一个字符数组的指针,该数组存储了要读取的字符串。
  • n – 这是要读取的最大字符数(包括最后的空字符)。通常是使用以 str 传递的数组长度。
  • stream – 这是指向 FILE 对象的指针,该 FILE 对象标识了要从中读取字符的流。

输出

1
puts(str);

string.h库

C 中有大量操作字符串的函数:

序号 函数 & 目的
1 strcpy(s1, s2); 复制字符串 s2 到字符串 s1。(后到前)
2 strcat(s1, s2); 连接字符串 s2 到字符串 s1 的末尾。
3 strlen(s1); 返回字符串 s1 的长度。
4 strcmp(s1, s2); 如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回小于 0;如果 s1>s2 则返回大于 0。
5 strchr(s1, ch); 返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
6 strstr(s1, s2); 返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。
7 strtok() Method: Splits str[] according to given delimiters and returns the next token. It needs to be called in a loop to get all tokens. It returns NULL when there are no more tokens.

islower(), isupper(), isalpha(), isdigit()

在第一次循环中,strtok函数将第一个人信息后的这个逗号,改为了’\0,这时strtok内部的this指针指向的是逗号的后一个字符。

而在第一个循环结束后,函数第一个参数被设定为NULL,strtok将以this指针指向的位置作为分解起始位置,此时this指针指向的是’\0’,strtok对一个空串无法切分,返回NULL,所以得到上面的结果。

正确返回大于0的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char str[]= “The quick brown fox”; // string to be searched
char ch= ‘q’; // The character we are looking for
char *pGot_char= NULL; // Pointer initialized to NULL
pGot_char= strchr(str,chr); // stores address where chis found


tokenize
// Declaration of string
char gfg[100] = "Geeks-for - geeks - Contribute";
// Declaration of delimiter
const char s[4] = "-";
char* tok;
// Use of strtok
// get first token
tok = strtok(gfg, s);
// Checks for delimiter
while (tok != 0) {
printf(" %s\n", tok);
// Use of strtok
// go through other tokens
tok = strtok(0, s);//!!!!!!千万注意这里是NULL或者0
}

define constant srings

1
#define MONTHS 12 

Important things to remember

– No semicolon at the end

– No equal sign used to assign values to string

– There is no such thing as local define since they can appear anywhere

in the program

– String Messages

• #define Message “You have won the game!”

指针与string

const int* ptr=&a;//指针指向地址会变

int *const ptr=&a;//不会变,变化时报错

交换字符串

第一行代码的意思是定义了一个char*类型的指针,我们将关注的是代码的存储问题,这里str变量是存储于栈区的,而”hello”, 则是存放在字符串常量区,字符串常量区的数据是不可更改的。所以无法直接更改“hello”和“world”的存储位置来达到交换的目的

正确的思路是,再分别定义一个指针来指向str1和str2,即指向指针的指针(二级指针)——把函数的参数定义为二级指针,作用是为了通过交换栈区的str1, 和str2存储的字符串所在内存空间的地址来达到目的


C语言Char和string
http://mavericreate.top/Blogs/2025/08/28/C语言Char和string/
作者
唐浩天
发布于
2025年8月28日
许可协议