当前位置:网站首页>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 (35) -- text preprocessing (NLP)
- Hands on deep learning (44) -- seq2seq principle and Implementation
- Basic data types in golang
- Log cannot be recorded after log4net is deployed to the server
- Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
- 品牌连锁店5G/4G无线组网方案
- Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)
- Hands on deep learning (32) -- fully connected convolutional neural network FCN
- Daughter love: frequency spectrum analysis of a piece of music
- Modules golang
猜你喜欢
PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
Hands on deep learning (37) -- cyclic neural network
Logstack configuration details -- elasticstack (elk) work notes 020
Hands on deep learning (33) -- style transfer
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
Latex download installation record
智能网关助力提高工业数据采集和利用
Ultimate bug finding method - two points
How can people not love the amazing design of XXL job
2022-2028 global optical transparency industry research and trend analysis report
随机推荐
5g/4g wireless networking scheme for brand chain stores
2022-2028 global intelligent interactive tablet industry research and trend analysis report
C language pointer interview question - the second bullet
Hands on deep learning (43) -- machine translation and its data construction
Get the source code in the mask with the help of shims
`Example of mask ` tool use
Golang defer
2022-2028 global small batch batch batch furnace industry research and trend analysis report
Hands on deep learning (35) -- text preprocessing (NLP)
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
查看CSDN个人资源下载明细
Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
自动化的优点有哪些?
View CSDN personal resource download details
About the for range traversal operation in channel in golang
Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
【leetcode】540. A single element in an ordered array
Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'
JDBC and MySQL database
MySQL develops small mall management system