当前位置:网站首页>【C语言】字符串左旋
【C语言】字符串左旋
2022-07-06 05:58:00 【SouLinya】
题目
实现一个函数,可以左旋字符串中的k个字符。
例如:
ABCD左旋一个字符得到BCDA
ABCD左旋两个字符得到CDAB
一、解法一:暴力求解法

#include<stdio.h>
#include<string.h>
void Swap_arr(char* arr, int k)
{
int len = strlen(arr);
int i = 0;
for (i = 0; i < k; i++)
{
//保留首元素地址
char temp = *arr;
int j = 0;
//字符前移
for (j = 0; j < len - 1; j++)
{
*(arr + j) = *(arr + j + 1);
}
//最后把结尾字符赋值成首元素
*(arr + len - 1) = temp;
}
}
int main()
{
char arr[] = "abcdef";
int k;
scanf("%d", &k);
Swap_arr(arr, k);
printf("%s", arr);
return 0;
}
二、解法二:三步翻转法
思路:
假设左旋两个字符
1.先逆序前面两个字符
bacdef
2.逆序后面四个字符
bafedc
3.逆序整体
cdefab
#include<stdio.h>
//字符串逆序函数
void reverse(char* left, char* right)
{
while (left < right)
{
char temp = *left;
*left = *right;
*right = temp;
left++;
right--;
}
}
void Swap_arr(char* arr, int k)
{
//计算字符串长度
int len = strlen(arr);
//逆序前面.注意传的参数
reverse(arr, arr + k - 1);
//逆序后面
reverse(arr + k, arr + len - 1);
//逆序整体
reverse(arr, arr + len - 1);
}
int main()
{
char arr[] = "abcdef";
int k;
scanf("%d", &k);
Swap_arr(arr, k);
printf("%s\n", arr);
return 0;
}
三. 解法三:拼接法
思路:利用strcpy字符串拷贝函数和strncat字符串追加函数
ab cdef ab cdef
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
void Swap_arr(char* arr, int k)
{
assert(arr != NULL);
int len = strlen(arr);
//断开位置的下标
int pos = k % len;
//动态内存开辟空间:len+1的原因是字符串还有'\0'字符
char* p = (char*)malloc((len+1) * sizeof(char));
if (p == NULL)
{
printf("%s\n", strerror(errno));
}
else
{
//先拷贝后面的cdef
strcpy(p, arr + pos);
//再拷贝前面的ab
strncat(p, arr, pos);
//拷贝到源字符串当中
strcpy(arr, p);
}
}
int main()
{
char arr[] = "abcdef";
int k;
scanf("%d", &k);
Swap_arr(arr, k);
printf("%s\n", arr);
return 0;
}
边栏推荐
- Luogu p1460 [usaco2.1] healthy Holstein cows
- ArcGIS application foundation 4 thematic map making
- Download, install and use NVM of node, and related use of node and NRM
- YYGH-11-定时统计
- CoDeSys note 2: set coil and reset coil
- 数学三大核心领域概述:代数
- 关于 PHP 启动 MongoDb 找不到指定模块问题
- Jushan database appears again in the gold fair to jointly build a new era of digital economy
- 【无标题】
- 公司視頻加速播放
猜你喜欢

华为BFD的配置规范

Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028

Usage of test macro of GTEST

Hongliao Technology: Liu qiangdong's "heavy hand"

(5) Explanation of yolo-v3 core source code (3)

How to use the container reflection method encapsulated by thinkphp5.1 in business code

授予渔,从0开始搭建一个自己想要的网页

局域网同一个网段通信过程

如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
[paper reading] nflowjs: synthetic negative data intensive anomaly detection based on robust learning
【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
Auto. JS learning notes 17: basic listening events and UI simple click event operations
(5) Explanation of yolo-v3 core source code (3)
进程和线程
Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
2022 software testing workflow to know
Luogu p1460 [usaco2.1] healthy Holstein cows
华为路由器忘记密码怎么恢复
查询生产订单中某个(些)工作中心对应的标准文本码
单元测试的意义
HCIA复习
AUTOSAR from getting started to becoming proficient (10) - embedded S19 file analysis
入侵检测领域数据集总结
如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
Station B, Mr. Liu Er - multiple logistic regression, structure 7
华为BFD的配置规范
H3C防火墙RBM+VRRP 组网配置
Redistemplate common collection instructions opsforvalue (II)
Introduction to promql of # yyds dry goods inventory # Prometheus