当前位置:网站首页>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;
}边栏推荐
- 法向量点云旋转
- About the for range traversal operation in channel in golang
- Hands on deep learning (40) -- short and long term memory network (LSTM)
- Kotlin 集合操作汇总
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
- Hands on deep learning (32) -- fully connected convolutional neural network FCN
- Log cannot be recorded after log4net is deployed to the server
- Kotlin set operation summary
- Web端自动化测试失败原因汇总
- Are there any principal guaranteed financial products in 2022?
猜你喜欢

Normal vector point cloud rotation

Four common methods of copying object attributes (summarize the highest efficiency)

2022-2028 global edible probiotic raw material industry research and trend analysis report

El Table Radio select and hide the select all box

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

Mmclassification annotation file generation

5g/4g wireless networking scheme for brand chain stores

Fabric of kubernetes CNI plug-in

Write a mobile date selector component by yourself

Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
随机推荐
Matlab tips (25) competitive neural network and SOM neural network
Write a mobile date selector component by yourself
Summary of the most comprehensive CTF web question ideas (updating)
2022-2028 global tensile strain sensor industry research and trend analysis report
Upgrading Xcode 12 caused Carthage to build cartfile containing only rxswift to fail
C语言指针面试题——第二弹
How does idea withdraw code from remote push
法向量点云旋转
Write a jison parser from scratch (1/10):jison, not JSON
Qtreeview+ custom model implementation example
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
2022-2028 research and trend analysis report on the global edible essence industry
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
MySQL develops small mall management system
Lauchpad X | 模式
Multilingual Wikipedia website source code development part II
Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
Kotlin:集合使用
xxl-job惊艳的设计,怎能叫人不爱
智能网关助力提高工业数据采集和利用