跳到主要内容

C语言字符串详解

1. 什么是字符串

在C语言中,字符串是以字符数组的形式存储的,并且以空字符 \0 结尾。C语言标准库提供了多种操作字符串的函数。字符串的声明方式如下:

char str1[] = "Hello, World!";
char str2[20] = "Hello, C!";
char str3[20]; // 仅声明,还未赋值

2. 字符串输入与输出

C语言中可以使用 printfscanf 函数来输出和输入字符串。

#include <stdio.h>

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str); // 注意:scanf 只能读取不含空格的字符串
printf("You entered: %s\n", str);
return 0;
}

读取包含空格的字符串

如果需要读取包含空格的字符串,可以使用 gets 函数或 fgets 函数。

#include <stdio.h>

int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // fgets 可以读取包含空格的字符串
printf("You entered: %s\n", str);
return 0;
}

3. 字符串长度

使用 strlen 函数可以获取字符串的长度(不包括 \0 )。

#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
int length = strlen(str);
printf("Length of the string: %d\n", length);
return 0;
}

4. 字符串复制

使用 strcpy 函数可以将一个字符串复制到另一个字符串中。

#include <stdio.h>
#include <string.h>

int main() {
char src[] = "Hello, World!";
char dest[50];
strcpy(dest, src);
printf("Destination string: %s\n", dest);
return 0;
}

5. 字符串连接

使用 strcat 函数可以将两个字符串连接在一起。

#include <stdio.h>
#include <string.h>

int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}

6. 字符串比较

使用 strcmp 函数可以比较两个字符串。如果相等,返回0;如果前者大于后者,返回正值;如果前者小于后者,返回负值。

#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\n");
} else if (result > 0) {
printf("str1 is greater than str2.\n");
} else {
printf("str1 is less than str2.\n");
}
return 0;
}

7. 查找子串

使用 strstr 函数可以在一个字符串中查找另一个字符串。

#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
char substr[] = "World";
char *position = strstr(str, substr);
if (position != NULL) {
printf("Substring found at position: %ld\n", position - str);
} else {
printf("Substring not found.\n");
}
return 0;
}

8. 字符串转换

C语言提供了一些函数将字符串转换为其他数据类型,如 atoi (将字符串转换为整数),atof (将字符串转换为浮点数) 等。

#include <stdio.h>
#include <stdlib.h>

int main() {
char str[] = "12345";
int num = atoi(str);
printf("Integer: %d\n", num);
return 0;
}

9. 字符串常用函数汇总

函数功能
strlen计算字符串的长度
strcpy复制字符串
strncpy复制字符串的前n个字符
strcat连接字符串
strncat连接字符串的前n个字符
strcmp比较字符串
strncmp比较字符串的前n个字符
strstr在字符串中查找子串
strchr在字符串中查找字符
strrchr从后向前在字符串中查找字符
strtok分割字符串
atoi将字符串转换为整数
atof将字符串转换为浮点数
sprintf格式化字符串并将其存储在另一个字符串中
sscanf从字符串中读取格式化输入并存储到变量中
fgets从文件或标准输入中读取一行并存储到字符串中
fputs将字符串写入文件或标准输出

10. 示例程序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
// 1. 字符串输入与输出
char str1[100];
printf("Enter a string: ");
fgets(str1, sizeof(str1), stdin);
printf("You entered: %s\n", str1);

// 2. 字符串长度
printf("Length of the string: %lu\n", strlen(str1));

// 3. 字符串复制
char str2[100];
strcpy(str2, str1);
printf("Copied string: %s\n", str2);

// 4. 字符串连接
char str3[100] = "Hello, ";
strcat(str3, str2);
printf("Concatenated string: %s\n", str3);

// 5. 字符串比较
char str4[] = "Hello";
int cmpResult = strcmp(str4, "Hello");
if (cmpResult == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}

// 6. 查找子串
char *pos = strstr(str3, "World");
if (pos) {
printf("Substring found at position: %ld\n", pos - str3);
} else {
printf("Substring not found.\n");
}

// 7. 字符串转换
char numStr[] = "12345";
int num = atoi(numStr);
printf("Converted number: %d\n", num);

return 0;
}