当前位置:网站首页>[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
2022-07-02 06:29:00 【Xiexishan】
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
Scheme 1
analysis :s1 Rotate once as ABCDA, and s2 Compare , call strcmp, The return value is equal to 0 Value , It means yes s2 yes s1 Spin to get , Otherwise, continue to ABCDA Rotate again on the basis of , obtain BCDAA, and s2 Compare , call strcmp, The return value is equal to 0 Value , It means yes s2 yes s1 Spin to get , Continue to cycle , Until the cycle 5 Time , Or just meet in the cycle s1=s2, It means that ,s2 yes s1 Spin to get .
# define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
int judge(char* s1, char* s2,int len)
{
// First rotate one character
for (int i = 1; i <= len; ++i)
{
//i by 1 First rotation
char tem = *s1;
int j=0;
for (j = 0; j < len; ++j)
{
*(s1 + j) = *(s1 + j + 1);
}
*(s1+j) = tem;// Put the value just taken out into the last element
if (strcmp(s1, s2) == 0)
{
return 1;
}
}
}
int main()
{
char s1[] = "AABCD";
char s2[] = "BCDAA";
int len = strlen(s1);
if (judge(s1,s2,len))
printf("Fing it");
else
printf("No find");
return 0;
}
programme 2
Using functions strcat Make a new string s3,AABCDAABCD, use s2 To traverse whether the comparison function is s1 Subfunction of , If so s2 yes s1 Spin to get .
strncat function
Append characters of a string.
char *strncat(char *strDest,constchar *strSource,size_tcount);
# define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
int FlipCheck(char* arr1, char* arr2)
{
if (strlen(arr1) != strlen(arr2))// If two strings are not equal in length , Then don't compare until it's not the result of rotation .
return 0;
strncat(arr1, arr1, strlen(arr1));//append a string
return strstr(arr1, arr2) != NULL;// Find substrings , If found, the return value is non 0
}
int main()
{
char arr1[20] = "AABCD";
char arr2[10] = "BCDAA";
if (FlipCheck(arr1, arr2))
printf("Find it");
else
printf("NO Find");
return 0;
}
I sent a video explaining , In the comment area , Come and see
Today's rubbish : That company called me again , Ah ah
边栏推荐
猜你喜欢
Sentinel Alibaba open source traffic protection component
Redis——热点key问题
自学table au
链表(线性结构)
ShardingSphere-JDBC篇
代码技巧——Controller参数注解@RequestParam
Introduce two automatic code generators to help improve work efficiency
Support new and old imperial CMS collection and warehousing tutorials
Linked list (linear structure)
日志(常用的日志框架)
随机推荐
重载全局和成员new/delete
数据科学【九】:SVD(二)
MySQL的10大经典错误
Don't use the new WP collection. Don't use WordPress collection without update
Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
Redis - cluster data distribution algorithm & hash slot
(第一百篇BLOG)写于博士二年级结束-20200818
Arduino Wire 库使用
Summary of advertisement business bug replay
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
ShardingSphere-JDBC篇
Android - Kotlin 下使用 Room 遇到 There are multiple good constructors and Room will ... 问题
深入学习JVM底层(二):HotSpot虚拟机对象
It is said that Kwai will pay for the Tiktok super fast version of the video? How can you miss this opportunity to collect wool?
代码技巧——Controller参数注解@RequestParam
Redis——大Key問題
CUDA中内置的Vector类型和变量
【程序员的自我修养]—找工作反思篇二
一起学习SQL中各种join以及它们的区别
In depth understanding of JUC concurrency (II) concurrency theory