一、字符串基本操作
1.1初始化
void test01(){
char str1[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
printf("%s\n",str1);
char str2[100] = { 'h', 'e', 'l', 'l', 'o' };
printf("%s\n", str2);
char str3[] = "hello";
printf("%s\n",str3);
printf("sizeof str:%d\n",sizeof(str3));
printf("strlen str:%d\n",strlen(str3));
char str4[100] = "hello";
printf("sizeof str:%d\n", sizeof(str4));
printf("strlen str:%d\n", strlen(str4));
char str5[] = "hello\0world";
printf("%s\n",str5);
printf("sizeof str5:%d\n",sizeof(str5));
printf("strlen str5:%d\n",strlen(str5));
char str6[] = "hello\012world";
printf("%s\n", str6);
printf("sizeof str6:%d\n", sizeof(str6));
printf("strlen str6:%d\n", strlen(str6));
}
1.2 字符串拷贝
void copy_string01(char* dest, char* source ){
for (int i = 0; source[i] != '\0';i++){
dest[i] = source[i];
}
}
void copy_string02(char* dest, char* source){
while (*source != '\0' ){
*dest = *source;
source++;
dest++;
}
}
void copy_string03(char* dest, char* source){
while (*dest++ = *source++){}
}
int copy_string04(char* dest, char* source){
if (dest == NULL){
return -1;
}
if (source == NULL){
return -2;
}
char* src = source;
char* tar = dest;
while (*tar++ = *src++){}
return 0;
}
二、字符串的格式化
2.1 sprintf
#include <stdio.h>
int sprintf(char *str, const char *format, ...);
功能:
根据参数format字符串来转换并格式化数据,然后将结果输出到str指定的空间中,直到 出现字符串结束符 '\0' 为止。
参数:
str:字符串首地址
format:字符串格式,用法和printf()一样
返回值:
成功:实际格式化的字符个数
失败: - 1
void test(){
char buf[1024] = { 0 };
sprintf(buf, "你好,%s,欢迎加入我们!", "John");
printf("buf:%s\n",buf);
memset(buf, 0, 1024);
sprintf(buf, "我今年%d岁了!", 20);
printf("buf:%s\n", buf);
memset(buf, 0, 1024);
char str1[] = "hello";
char str2[] = "world";
int len = sprintf(buf,"%s %s",str1,str2);
printf("buf:%s len:%d\n", buf,len);
memset(buf, 0, 1024);
int num = 100;
sprintf(buf, "%d", num);
printf("buf:%s\n", buf);
memset(buf, 0, 1024);
sprintf(buf, "%8d", num);
printf("buf:%s\n", buf);
memset(buf, 0, 1024);
sprintf(buf, "%-8d", num);
printf("buf:%s\n", buf);
memset(buf, 0, 1024);
sprintf(buf, "0x%x", num);
printf("buf:%s\n", buf);
memset(buf, 0, 1024);
sprintf(buf, "0%o", num);
printf("buf:%s\n", buf);
}
2.2 sscanf
#include <stdio.h>
int sscanf(const char *str, const char *format, ...);
功能:
从str指定的字符串读取数据,并根据参数format字符串来转换并格式化数据。
参数:
str:指定的字符串首地址
format:字符串格式,用法和scanf()一样
返回值:
成功:实际读取的字符个数
失败: - 1
格式 | 作用 |
---|
%*s或%*d | 跳过数据 |
%[width]s | 读指定宽度的数据 |
%[a-z] | 匹配a到z中任意字符(尽可能多的匹配) |
%[aBc] | 匹配a、B、c中一员,贪婪性 |
%[^a] | 匹配非a的任意字符,贪婪性 |
%[^a-z] | 表示读取除a-z以外的所有字符 |
void test01(){
char buf[1024] = { 0 };
sscanf("123456aaaa", "%*d%s", buf);
printf("buf:%s\n",buf);
}
void test02(){
char buf[1024] = { 0 };
sscanf("123456aaaa", "%7s", buf);
printf("buf:%s\n", buf);
}
void test03(){
char buf[1024] = { 0 };
sscanf("abcdefg123456", "%[a-z]", buf);
printf("buf:%s\n", buf);
}
void test04(){
char buf[1024] = { 0 };
sscanf("abcdefg123456", "%[aBc]", buf);
printf("buf:%s\n", buf);
}
void test05(){
char buf[1024] = { 0 };
sscanf("bcdefag123456", "%[^a]", buf);
printf("buf:%s\n", buf);
}
void test06(){
char buf[1024] = { 0 };
sscanf("123456ABCDbcdefag", "%[^a-z]", buf);
printf("buf:%s\n", buf);
}