当前位置:网站首页>2021-08-10 character pointer
2021-08-10 character pointer
2022-07-04 09:47:00 【skeet follower】
1. The string is left-handed
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
analysis : Let's first consider the left rotation of a character and then proceed for Cycle the number of left turns that meet the requirements of the topic , First put the characters ‘A’ Save up , And then 'BCD‘ Move forward one by one , Finally, the ’A‘ Put it in the last place to meet the condition , Let's do the following .
The code is as follows :
void my_leftmove(char* arr, int m)
{
int len = strlen(arr);
int i,j;
for (i = 0; i < m; i++)
{
// Rotate one character
//1. Save the first character
char p = *arr;
//2. Move the following characters forward in turn
for (j = 0; j < len - 1; j++){
*(arr + j) = *(arr + j + 1);
}
//3. Put the saved characters in the last place
*(arr + len - 1) = p;
}
}
int main()
{
char arr[] = "ABCD";
int k;
scanf("%d", &k);
my_leftmove(arr, k);
printf("%s", arr);
return 0;
}2.
String rotation result
Write a function , To determine whether a string is the rotated string of another string .
for example : Given s1 =AABCD and s2 = BCDAA, return 1
Given s1=abcd and s2=ACBD, return 0.
AABCD Turn a character left to get ABCDA
AABCD Two left-handed characters get BCDAA
AABCD Turn one character right to get DAABC
Code 1 as follows :
#include<stdio.h>
void my_leftmove(char* arr, int m)
{
int len = strlen(arr);
int i,j;
for (i = 0; i < m; i++)
{
// Rotate one character
//1. Save the first character
char p = *arr;
//2. Move the following characters forward in turn
for (j = 0; j < len - 1; j++){
*(arr + j) = *(arr + j + 1);
}
//3. Put the saved characters in the last place
*(arr + len - 1) = p;
}
}
int is_moveleft(char a1[], char a2[])
{
int len1= strlen(a1);
int len2 = strlen(a2);
// First determine whether the string length is equal , If it is not equal, it will return to ;
if (len1 != len2) {
return 0;
}
int i=0;
for (i = 0; i < len1; i++) {
// Shift string left
my_leftmove(a1, 1);
// Compare whether two arrays are equal ;
if (strcmp(a1, a2) == 0) {
return 1;
}
}
return 0;
}
int main()
{
char a1[] = "ABCDE";
char a2[] = "BCD";
int ret = is_moveleft(a1,a2);
if (ret == 1) {
printf("YES\n");
}
else {
printf("NO\n");
}
return 0;
}Code 2 as follows :
#include<stdio.h>
#include<assert.h>
#include<assert.h>
int is_moveleft(char a1[], char a2[]) {
// Determine whether it is null
assert(a1);
assert(a2);
int len1 = strlen(a1);
int len2 = strlen(a2);
if (len1 != len2) {
return 0;
}
int i = 0;
// to a1 Add a... To the back a1 String
strncat(a1, a1, len1);
// Judge a2 Whether it is a1 String
if (NULL == strstr(a1, a2)) {
return 0;
}
else {
return 1;
}
}
int main()
{
char a1[] = "ABCDE";
char a2[] = "BCDEA";
int ret = is_moveleft(a1,a2);
if (ret == 1) {
printf("YES\n");
}
else {
printf("NO\n");
}
return 0;
}边栏推荐
- Hands on deep learning (33) -- style transfer
- Explanation of for loop in golang
- mmclassification 标注文件生成
- Matlab tips (25) competitive neural network and SOM neural network
- 【leetcode】540. A single element in an ordered array
- Summary of the most comprehensive CTF web question ideas (updating)
- DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
- C language pointer interview question - the second bullet
- 2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
- Golang defer
猜你喜欢

MySQL foundation 02 - installing MySQL in non docker version
What are the advantages of automation?

2022-2028 research and trend analysis report on the global edible essence industry

Machine learning -- neural network (IV): BP neural network

Ultimate bug finding method - two points

libmysqlclient.so.20: cannot open shared object file: No such file or directory

mmclassification 标注文件生成

智慧路灯杆水库区安全监测应用

Regular expression (I)

Logstack configuration details -- elasticstack (elk) work notes 020
随机推荐
直方图均衡化
How can people not love the amazing design of XXL job
Summary of the most comprehensive CTF web question ideas (updating)
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
2022-2028 global small batch batch batch furnace industry research and trend analysis report
Kotlin:集合使用
Hands on deep learning (39) -- gating cycle unit Gru
Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
H5 audio tag custom style modification and adding playback control events
Nuxt reports an error: render function or template not defined in component: anonymous
View CSDN personal resource download details
JDBC and MySQL database
Golang defer
How to display √ 2 on the command line terminal ̅? This is actually a blog's Unicode test article
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
Get the source code in the mask with the help of shims
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
Explanation of closures in golang
Exercise 9-5 address book sorting (20 points)
lolcat