C语言常用字符串函数
·使用时注意事项:
确保目标缓冲区足够大
处理字符串终止符'\0'
检查函数返回值
使用安全版本函数(如strncpy替代strcpy)防止缓冲区溢出
下面是为每个常用C语言字符串函数提供的示例代码:
1. strlen()
- 字符串长度
#include <stdio.h>
#include <string.h>int main() {const char *str = "Hello, World!";size_t len = strlen(str);printf("Length of '%s' is %zu\n", str, len);return 0;
}
// 输出: Length of 'Hello, World!' is 13
2. strcpy()
- 字符串复制
#include <stdio.h>
#include <string.h>int main() {char dest[20];const char *src = "Copy this";strcpy(dest, src);printf("Copied string: %s\n", dest);return 0;
}
// 输出: Copied string: Copy this
3. strncpy()
- 安全字符串复制
#include <stdio.h>
#include <string.h>int main() {char dest[10];const char *src = "This is too long";strncpy(dest, src, sizeof(dest) - 1);dest[sizeof(dest) - 1] = '\0'; // 确保字符串终止printf("Safe copy: %s\n", dest);return 0;
}
// 输出: Safe copy: This is t
4. strcat()
- 字符串连接
#include <stdio.h>
#include <string.h>int main() {char dest[50] = "Hello, ";const char *src = "World!";strcat(dest, src);printf("Concatenated: %s\n", dest);return 0;
}
// 输出: Concatenated: Hello, World!
5. strncat()
- 安全字符串连接
#include <stdio.h>
#include <string.h>int main() {char dest[20] = "Hello";const char *src = ", World! This is too long";strncat(dest, src, sizeof(dest) - strlen(dest) - 1);printf("Safe concatenation: %s\n", dest);return 0;
}
// 输出: Safe concatenation: Hello, World!
6. strcmp()
- 字符串比较
#include <stdio.h>
#include <string.h>int main() {const char *str1 = "apple";const char *str2 = "banana";const char *str3 = "apple";printf("Compare 'apple' and 'banana': %d\n", strcmp(str1, str2));printf("Compare 'apple' and 'apple': %d\n", strcmp(str1, str3));return 0;
}
// 输出:
// Compare 'apple' and 'banana': -1 (或负数)
// Compare 'apple' and 'apple': 0
7. strncmp()
- 安全字符串比较
#include <stdio.h>
#include <string.h>int main() {const char *str1 = "apple pie";const char *str2 = "apple tart";// 只比较前5个字符int result = strncmp(str1, str2, 5);printf("Compare first 5 chars: %d\n", result);return 0;
}
// 输出: Compare first 5 chars: 0 (相同)
8. strchr()
- 查找字符首次出现
#include <stdio.h>
#include <string.h>int main() {const char *str = "Hello, World!";char *found = strchr(str, 'W');if (found) {printf("Found 'W' at position: %ld\n", found - str);} else {printf("Character not found\n");}return 0;
}
// 输出: Found 'W' at position: 7
9. strrchr()
- 查找字符最后出现
#include <stdio.h>
#include <string.h>int main() {const char *str = "Hello, World!";char *found = strrchr(str, 'l');if (found) {printf("Last 'l' at position: %ld\n", found - str);} else {printf("Character not found\n");}return 0;
}
// 输出: Last 'l' at position: 10
10. strstr()
- 查找子字符串
#include <stdio.h>
#include <string.h>int main() {const char *str = "The quick brown fox jumps over the lazy dog";const char *sub = "fox";char *found = strstr(str, sub);if (found) {printf("Found '%s' at position: %ld\n", sub, found - str);} else {printf("Substring not found\n");}return 0;
}
// 输出: Found 'fox' at position: 16
11. strpbrk()
- 查找任意匹配字符
#include <stdio.h>
#include <string.h>int main() {const char *str = "Hello, World!";const char *chars = "aeiou";char *found = strpbrk(str, chars);if (found) {printf("First vowel '%c' at position: %ld\n", *found, found - str);} else {printf("No vowels found\n");}return 0;
}
// 输出: First vowel 'e' at position: 1
12. strtok()
- 字符串分割
#include <stdio.h>
#include <string.h>int main() {char str[] = "apple,banana,cherry";const char *delim = ",";char *token = strtok(str, delim);while (token != NULL) {printf("Token: %s\n", token);token = strtok(NULL, delim);}return 0;
}
// 输出:
// Token: apple
// Token: banana
// Token: cherry
13. memcpy()
- 内存复制
#include <stdio.h>
#include <string.h>int main() {char src[] = "Copy this";char dest[20];memcpy(dest, src, strlen(src) + 1); // +1 包含终止符printf("Copied: %s\n", dest);return 0;
}
// 输出: Copied: Copy this
14. memmove()
- 安全内存复制(处理重叠)
#include <stdio.h>
#include <string.h>int main() {char str[] = "memmove can handle overlap";// 将"move"移动到开头memmove(str, str + 3, 4);printf("Result: %s\n", str);return 0;
}
// 输出: Result: move can handle overlap
15. memset()
- 内存设置
#include <stdio.h>
#include <string.h>int main() {char buffer[20];// 用'A'填充缓冲区memset(buffer, 'A', sizeof(buffer) - 1);buffer[sizeof(buffer) - 1] = '\0'; // 终止字符串printf("Buffer: %s\n", buffer);return 0;
}
// 输出: Buffer: AAAAAAAAAAAAAAAAAAA
16. memcmp()
- 内存比较
#include <stdio.h>
#include <string.h>int main() {char str1[] = "apple";char str2[] = "apples";int result = memcmp(str1, str2, 5); // 比较前5个字节printf("Compare first 5 bytes: %d\n", result);result = memcmp(str1, str2, 6); // 比较前6个字节printf("Compare first 6 bytes: %d\n", result);return 0;
}
// 输出:
// Compare first 5 bytes: 0
// Compare first 6 bytes: -1 (或负数)