当前位置:网站首页>String intensive training - copy string | string inversion implementation
String intensive training - copy string | string inversion implementation
2022-04-22 12:57:00 【Scarlett2025】
- A string has an end flag \0
- There are three ways to copy strings
- utilize []
- Use the pointer
- utilize while (*dest++ = *source++){}
- There are two ways to reverse a string
- utilize []
- Use the pointer
String basic operation
void test01()
{
// Note that the string needs to have an end sign '\0'
char str1[] = { 'h', 'e', 'l', 'l', 'o','\0' };
printf("str1:%s\n", str1);
}

void test01(){
// Character array part initialization , Residual filling 0
char str2[100] = { 'h', 'e', 'l', 'l', 'o' };
printf("str2:%s\n", str2);
}

void test01(){
// If initialized as a string , Then the compiler will add... At the end of the string by default '\0'
char str3[] = "hello";
printf("str3:%s\n", str3);
printf("sizeof str3:%d\n", sizeof(str3)); // Statistics \0
printf("strlen str3:%d\n", strlen(str3)); // No statistics \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 It's octal. Down decimal 10, stay ASCII In the table is a newline
printf("str6: %s\n", str6);
printf("sizeof str6:%d\n", sizeof(str6)); // 12
printf("strlen str6:%d\n", strlen(str6)); // 11
}

String copy function implementation
// Parameters 1 Target string Parameters 2 The source string
// demand The contents of the source string Copy to target string
// The first one is utilize [] The way Copy
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);
}
// The second kind Copy with string pointer
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);
}
// The third way
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);
}
String inversion model
The first way utilize [] Reverse
void reverseString01(char * str)
{
int len = strlen(str);
// Start position subscript
int start = 0;
// Subscript of end position
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);
}
The second way Use the pointer
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://yzsam.com/2022/04/202204221255444380.html
边栏推荐
- 动态规划 字符的最短距离
- Leetcode 83. Delete duplicate elements in the sorting linked list
- C#中如何将图片添加为程序的资源
- 利用OpenCV的函数threshold()对图像作基于OTSU的阈值化处理---并附比较好的介绍OTSU原理的博文链接
- 2022年四月21号S S M 框架整合@第一课 S S M 环境配置&重在实操。实操中总结。这里不展示结果。
- 百度地图结合vr全景,全景地图模式让你出行更放心
- Sqlserver游标的使用
- VR全景婚礼给你别样的浪漫,记录婚礼的甜蜜瞬间
- Desai wisdom number - line chart (dynamic line chart): price adjustment of gasoline and diesel in recent 1 year
- 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
猜你喜欢

Matlab bridge span combination problem GUI Graphical interface is completed

Under the pressure of sanctions, Russia plans to import electronic products in parallel through friendly countries

诺瓦星云更新招股书,继续上市进程,“学术派”的步步为盈?

数学——协方差

Today's sleep quality record 76 points

Secondary development of ABAQUS RSG plug-in (II)

396. 旋轉函數

Book city project registration page and email verification

【生活杂谈】中体平台教你如何提高彩票中奖率

ORB_ Slam3 learning: introduction to tracking thread
随机推荐
RxBus与RxBus2理解
11. 盛最多水的容器
JS foundation 6
JS box moves with the mouse when clicked
Leetcode 206, reverse linked list
Interpreting closures from structured context
vnpy本地導入csv數據
R language changes dataframe data from long to wide: spread function of tidyr package and pivot of CDATA package_ to_ Rowrecs function, data Table package dcast function
The R language uses the treemap function in the treemap package to visualize the treemap graph: treemap displays the hierarchical data as a group of nested rectangles, customized label color, label ba
Creation of multithreaded basic thread
深度学习笔记(一 误差)
Share the small problems you have encountered in writing projects recently
每日一题系列:公共子串计算
C# 是否管理员权限运行运行软件
摆脱 AI 生产“小作坊”:如何基于 Kubernetes 构建云原生 AI 平台
可执行文件的生成过程
Embedded development: three techniques for verifying sensors and communication data in embedded systems
Novartis Nebula updated its prospectus and continued its listing process. Is the "academic school" gaining momentum step by step?
Shrinking ox: the last Carnival
Leetcode 118, Yanghui triangle