当前位置:网站首页>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;
}
边栏推荐
- The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
- How does idea withdraw code from remote push
- IIS configure FTP website
- Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
- Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
- Kotlin 集合操作汇总
- Hands on deep learning (32) -- fully connected convolutional neural network FCN
- Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
- Development trend and market demand analysis report of high purity tin chloride in the world and China Ⓔ 2022 ~ 2027
- Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'
猜你喜欢
How does idea withdraw code from remote push
Fabric of kubernetes CNI plug-in
智能网关助力提高工业数据采集和利用
PHP student achievement management system, the database uses mysql, including source code and database SQL files, with the login management function of students and teachers
Hands on deep learning (36) -- language model and data set
Hands on deep learning (43) -- machine translation and its data construction
el-table单选并隐藏全选框
ASP. Net to access directory files outside the project website
PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql
C language pointer classic interview question - the first bullet
随机推荐
Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql
智慧路灯杆水库区安全监测应用
Hands on deep learning (43) -- machine translation and its data construction
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
Go context basic introduction
Write a jison parser from scratch (3/10): a good beginning is half the success -- "politics" (Aristotle)
How does idea withdraw code from remote push
Hands on deep learning (36) -- language model and data set
Qtreeview+ custom model implementation example
Application of safety monitoring in zhizhilu Denggan reservoir area
Solution to null JSON after serialization in golang
xxl-job惊艳的设计,怎能叫人不爱
What are the advantages of automation?
C language pointer interview question - the second bullet
Hands on deep learning (40) -- short and long term memory network (LSTM)
MySQL transaction mvcc principle
Write a jison parser from scratch (2/10): learn the correct posture of the parser generator parser generator
The child container margin top acts on the parent container
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share