在 C 语言中,字符串是以字符数组的形式表示的,以空字符 '\0'
结尾。C 语言提供了一系列的字符串处理函数,可以用于字符串的操作、查找、比较等。以下是一些常用的 C 语言字符串处理函数:
1. 字符串定义与初始化
#include <stdio.h>
#include <string.h>int main() {// 字符数组表示字符串char str1[10] = "Hello";char str2[] = {'W', 'o', 'r', 'l', 'd', '\0'};// 字符串常量char *str3 = "C Programming";printf("str1: %s\n", str1);printf("str2: %s\n", str2);printf("str3: %s\n", str3);return 0;
}
2. 字符串长度
#include <stdio.h>
#include <string.h>int main() {char str[] = "Hello, World!";int len = strlen(str);printf("Length of str: %d\n", len);return 0;
}
3. 字符串拷贝
#include <stdio.h>
#include <string.h>int main() {char source[] = "Hello, World!";char destination[20];// 使用 strcpy 进行拷贝strcpy(destination, source);printf("Source: %s\n", source);printf("Destination: %s\n", destination);return 0;
}
4. 字符串连接
#include <stdio.h>
#include <string.h>int main() {char str1[] = "Hello, ";char str2[] = "World!";// 使用 strcat 进行连接strcat(str1, str2);printf("Concatenated string: %s\n", str1);return 0;
}
5. 字符串比较
#include <stdio.h>
#include <string.h>int main() {char str1[] = "apple";char str2[] = "banana";// 使用 strcmp 进行比较int result = strcmp(str1, str2);if (result < 0) {printf("str1 is less than str2\n");} else if (result > 0) {printf("str1 is greater than str2\n");} else {printf("str1 is equal to str2\n");}return 0;
}
6. 字符串查找
#include <stdio.h>
#include <string.h>int main() {char str[] = "Hello, World!";char *ptr;// 使用 strstr 进行查找ptr = strstr(str, "World");if (ptr != NULL) {printf("Substring found at position: %ld\n", ptr - str);} else {printf("Substring not found\n");}return 0;
}
7. 字符串分割
C 语言本身不提供直接的字符串分割函数,但可以使用 strtok
函数来实现字符串分割。
#include <stdio.h>
#include <string.h>int main() {char str[] = "apple,orange,banana";char *token;// 使用 strtok 进行分割token = strtok(str, ",");while (token != NULL) {printf("Token: %s\n", token);token = strtok(NULL, ",");}return 0;
}
8. 字符串转换
字符串转整数
使用 atoi
或 strtol
函数将字符串转换为整数。
#include <stdio.h>
#include <stdlib.h>int main() {char str[] = "12345";int num1 = atoi(str);// 或者使用 strtolchar *endptr;long num2 = strtol(str, &endptr, 10);printf("Converted Integer (atoi): %d\n", num1);printf("Converted Integer (strtol): %ld\n", num2);return 0;
}
整数转字符串
使用 sprintf
或 snprintf
函数将整数转换为字符串。
#include <stdio.h>int main() {int num = 12345;char str1[20];char str2[20];// 使用 sprintfsprintf(str1, "%d", num);// 或者使用 snprintfsnprintf(str2, sizeof(str2), "%d", num);printf("Converted String (sprintf): %s\n", str1);printf("Converted String (snprintf): %s\n", str2);return 0;
}
9. 字符串操作的安全性考虑
在进行字符串操作时,要注意防止缓冲区溢出和安全漏洞。推荐使用带有长度参数的安全版本函数。
strncpy
替代strcpy
strncat
替代strcat
strncmp
替代strcmp
#include <stdio.h>
#include <string.h>int main() {char dest[10] = "Hello";char src[] = "World";// 使用 strncpy 避免缓冲区溢出strncpy(dest, src, sizeof(dest) - 1);dest[sizeof(dest) - 1] = '\0';printf("Safe Concatenation: %s\n", dest);return 0;
}
10. 动态内存分配和释放字符串
使用 malloc
和 free
进行字符串的动态内存分配和释放。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main() {char *str;// 动态分配内存str = (char *)malloc(20);if (str == NULL) {perror("Memory allocation failed");exit(EXIT_FAILURE);}// 操作字符串strcpy(str, "Dynamic Memory");// 使用字符串// 释放内存free(str);return 0;
}
在实际的 C 语言编程中,字符串处理是非常常见的任务之一。除了上述介绍的函数外,还有许多其他字符串处理函数,开发者可以根据具体需求选择合适的函数进行使用。
👇👇👇👇点击最下方名片: Linux兵工厂,免费领取Linux硬核学习资料👇👇👇👇