当前位置:网站首页>字符串强化训练-拷贝字符串|字符串反转实现
字符串强化训练-拷贝字符串|字符串反转实现
2022-04-22 12:56:00 【Scarlett2025】
- 字符串是有结束标志 \0
- 利用三种方式对字符串进行拷贝
- 利用[]
- 利用指针
- 利用while (*dest++ = *source++){}
- 利用两种方式对字符串进行反转
- 利用[]
- 利用指针
字符串基本操作
void test01()
{
//注意字符串需要有结束的标志 '\0'
char str1[] = { 'h', 'e', 'l', 'l', 'o','\0' };
printf("str1:%s\n", str1);
}

void test01(){
//字符数组部分初始化,剩余填0
char str2[100] = { 'h', 'e', 'l', 'l', 'o' };
printf("str2:%s\n", str2);
}

void test01(){
//如果以字符串初始化,那么编译器默认会在字符串尾部添加'\0'
char str3[] = "hello";
printf("str3:%s\n", str3);
printf("sizeof str3:%d\n", sizeof(str3)); //统计 \0
printf("strlen str3:%d\n", strlen(str3)); //不统计 \0
}

void test01(){
char str4[100] = "hello";
printf("sizeof str4:%d\n", sizeof(str4)); // 100
printf("strlen str4:%d\n", strlen(str4)); // 5
}

void test01(){
char str5[] = "hello\0world";
printf("str5:%s\n", str5);
printf("sizeof str5:%d\n", sizeof(str5)); //12
printf("strlen str5:%d\n", strlen(str5)); //5
}

void test01(){
char str6[] = "hello\012world"; // \012 是八进制 下转十进制的10,在ASCII表中是换行
printf("str6: %s\n", str6);
printf("sizeof str6:%d\n", sizeof(str6)); // 12
printf("strlen str6:%d\n", strlen(str6)); // 11
}

字符串拷贝功能实现
// 参数1 目标字符串 参数2 源字符串
// 需求 将源字符串中的内容 拷贝到目标字符串中
// 第一种 利用[]方式 进行拷贝
void copyString01(char * dest, char * source)
{
int len = strlen(source);
for (int i = 0; i < len; i++)
{
dest[i] = source[i];
}
dest[len] = '\0';
}
void test02()
{
char * str = "hello world";
char buf[1024];
copyString01(buf, str);
printf("%s\n", buf);
}
//第二种 利用字符串指针进行拷贝
void copyString02(char * dest, char * source)
{
while (*source != '\0')
{
*dest = *source;
dest++;
source++;
}
*dest = '\0';
}
void test02()
{
char * str = "hello world";
char buf[1024];
copyString02(buf, str);
printf("%s\n", buf);
}
//第三种方式
void copyString03(char * dest, char * source)
{
while (*dest++ = *source++) {}
/*int a = 0;
while (a = 0)
{
printf("a");
}*/
}
void test02()
{
char * str = "hello world";
char buf[1024];
copyString03(buf, str);
printf("%s\n", buf);
}
字符串反转模型
第一种方式 利用[]进行反转
void reverseString01(char * str)
{
int len = strlen(str);
//起始位置下标
int start = 0;
//结束位置的下标
int end = len - 1;
while (start < end)
{
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
void test03()
{
char str[] = "abcdefg";
reverseString01(str);
printf("%s\n", str);
}
第二种方式 利用指针
void reverseString02(char * str)
{
int len = strlen(str);
char * start = str;
char * end = str + len - 1;
while (start < end)
{
char temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
void test03()
{
char str[] = "abcdefg";
reverseString02(str);
printf("%s\n", str);
}
版权声明
本文为[Scarlett2025]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Scarlett2025/article/details/124285242
边栏推荐
- IDE導入項目
- R language ggplot2 visualizes the aspect ratio of fixed images_ Fixed() function
- Select the appropriate span combination for one span in MATLAB Bridge
- 17. Letter combination of telephone number
- Smart cultural tourism is gradually digitalized, and VR panorama promotes the integrated development of cultural tourism
- Today's sleep quality record 76 points
- 四种方式实现对OpenCV的MAT类矩阵元素的遍历
- JS foundation 10
- R语言使用write_xlsx函数导出、写入dataframe数据到excel文件实战
- Solve the problem that CMD commands cannot be executed continuously
猜你喜欢

Desai wisdom number - line chart (dynamic line chart): price adjustment of gasoline and diesel in recent 1 year

迪赛智慧数——折线图(动态折线图):近1年汽柴油调价情况

一句代码将OpenCV的Mat对象的数据打印输出

vnpy本地导入csv数据

Shopping form making

Share the small problems you have encountered in writing projects recently

Five ways to get database connection: have you really got it?

C#之SQL数据库操作(源码)

百度地图结合vr全景,全景地图模式让你出行更放心

Smart cultural tourism is gradually digitalized, and VR panorama promotes the integrated development of cultural tourism
随机推荐
多线程基础一 线程的创建
[jz48 longest substring without repeated characters]
缩表牛:最后的狂欢
书城项目注册页面和邮箱验证
R language ggplot2 visualization: calculate the number of missing values of each data column in the dataframe, and visualize the missing values of each data column using a stacked bar plot (set the co
JS foundation 9
[life gossip] Chinese sports platform teaches you how to improve the lottery winning rate
. net treasure API: outputformatter, format output object
Page d'inscription du projet Bookstore et vérification de la boîte aux lettres
51单片机之串口通信详解及代码示例
17. Letter combination of telephone number
VR panorama truly restores the driving school environment, and VR live shows the hard power of the driving school
Book city project registration page and email verification
R language ggplot2 visualizes the custom sorting of x-axis factor variables instead of sorting in the default alphabetical order
Leetcode 1768, alternate merge string
C#自定义Button实现源码
JS foundation 14
IDE导入项目
挑选了适合测试边界的汉字及截图
百度地图结合vr全景,全景地图模式让你出行更放心