当前位置:网站首页>[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;
}
边栏推荐
- [untitled]
- H3C firewall rbm+vrrp networking configuration
- C language bubble sort
- Raised a kitten
- Understanding of processes and threads
- Detailed explanation of BF and KMP
- 公司视频加速播放
- isam2运行流程
- Cannot create PoolableConnectionFactory (Could not create connection to database server. 错误
- [Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS
猜你喜欢
Hongliao Technology: how to quickly improve Tiktok store
How to use the container reflection method encapsulated by thinkphp5.1 in business code
【Postman】Collections-运行配置之导入数据文件
【C语言】字符串左旋
properties文件
黑猫带你学UFS协议第4篇:UFS协议栈详解
把el-tree选中的数组转换为数组对象
SQLMAP使用教程(三)实战技巧二
How Huawei routers configure static routes
Application du Groupe Li dans gtsam
随机推荐
数学三大核心领域概述:几何
ICLR 2022 spotlight | analog transformer: time series anomaly detection method based on correlation difference
Fault, error, failure of functional safety
Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028
How Huawei routers configure static routes
Database: ODBC remote access SQL Server2008 in oracel
Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
Overview of three core areas of Mathematics: algebra
GTSAM中ISAM2和IncrementalFixedLagSmoother说明
Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
Gtest之TEST宏的用法
黑猫带你学UFS协议第4篇:UFS协议栈详解
養了只小猫咪
Grant Yu, build a web page you want from 0
LeetCode 729. 我的日程安排表 I
C language bubble sort
Understanding of processes and threads
通过修改style设置打印页样式
Detailed explanation of BF and KMP
【Postman】Monitors 监测API可定时周期运行