当前位置:网站首页>[C language] string left rotation
[C language] string left rotation
2022-07-06 06:11:00 【SouLinya】
List of articles
subject
Implement a function , You can rotate left in a string k Characters .
for example :
ABCD Turn a character left to get BCDA
ABCD Two left-handed characters get CDAB
One 、 Solution 1 : Violent solution

#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++)
{
// Keep the first element address
char temp = *arr;
int j = 0;
// Character forward
for (j = 0; j < len - 1; j++)
{
*(arr + j) = *(arr + j + 1);
}
// Finally, assign the end character to the first element
*(arr + len - 1) = temp;
}
}
int main()
{
char arr[] = "abcdef";
int k;
scanf("%d", &k);
Swap_arr(arr, k);
printf("%s", arr);
return 0;
}
Two 、 Solution 2 : Three step flipping
Ideas :
Suppose there are two left-handed characters
1. First reverse the first two characters
bacdef
2. The last four characters in reverse order
bafedc
3. The whole in reverse order
cdefab
#include<stdio.h>
// String reverse order function
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)
{
// Calculate string length
int len = strlen(arr);
// In reverse order . Pay attention to the transmitted parameters
reverse(arr, arr + k - 1);
// After reverse order
reverse(arr + k, arr + len - 1);
// The whole in reverse order
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;
}
3、 ... and . Solution 3 : Splicing
Ideas : utilize strcpy String copy function and strncat String append function
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);
// Subscript of off position
int pos = k % len;
// Dynamic memory opens up space :len+1 The reason is that strings and '\0' character
char* p = (char*)malloc((len+1) * sizeof(char));
if (p == NULL)
{
printf("%s\n", strerror(errno));
}
else
{
// Copy the following first cdef
strcpy(p, arr + pos);
// Copy the previous ab
strncat(p, arr, pos);
// Copy to the source string
strcpy(arr, p);
}
}
int main()
{
char arr[] = "abcdef";
int k;
scanf("%d", &k);
Swap_arr(arr, k);
printf("%s\n", arr);
return 0;
}
边栏推荐
- IPv6 comprehensive experiment
- [postman] the monitors monitoring API can run periodically
- Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028
- Embedded point test of app
- nodejs实现微博第三方登录
- [API interface tool] Introduction to postman interface
- 数字三角形模型 AcWing 1015. 摘花生
- GTSAM中李群的运用
- The usage and difference between strlen and sizeof
- Testing and debugging of multithreaded applications
猜你喜欢
![[postman] test script writing and assertion details](/img/65/6520fe78bb2b3ff99f16d09ea8c0d1.png)
[postman] test script writing and assertion details

What are the test sites for tunnel engineering?

How Huawei routers configure static routes

黑猫带你学UFS协议第4篇:UFS协议栈详解

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

nodejs实现微博第三方登录

MySQL之数据类型

LeetCode 732. 我的日程安排表 III

MySQL之基础知识

P问题、NP问题、NPC问题、NP-hard问题详解
随机推荐
[Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS
A complete collection of necessary learning websites for office programmers
Seven imperceptible truths in software testing
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
IPv6 comprehensive experiment
进程和线程的理解
《卓有成效的管理者》读书笔记
nodejs实现微博第三方登录
Redis6 cluster setup
Clock in during winter vacation
IDEA 新UI使用
Hongliao Technology: how to quickly improve Tiktok store
MPLS test report
【课程笔记】编译原理
What are the test sites for tunnel engineering?
10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块
LeetCode 1200. 最小绝对差
异常检测方法总结
多线程应用的测试与调试
JDBC Requset 对应内容及功能介绍