当前位置:网站首页>[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
边栏推荐
- TensorRT的命令行程序
- 【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
- Three suggestions for all students who have graduated and will graduate
- 【程序员的自我修养]—找工作反思篇二
- Introduce two automatic code generators to help improve work efficiency
- Redis - hot key issues
- 重载全局和成员new/delete
- Redis——缓存击穿、穿透、雪崩
- 介绍两款代码自动生成器,帮助提升工作效率
- unittest.TextTestRunner不生成txt测试报告
猜你喜欢
随机推荐
CUDA中的动态全局内存分配和操作
virtualenv和pipenv安装
Code skills - Controller Parameter annotation @requestparam
LeetCode 77. combination
CUDA中的异步数据拷贝
Log (common log framework)
标签属性disabled selected checked等布尔类型赋值不生效?
广告业务Bug复盘总结
Cglib agent - Code enhancement test
TensorRT的功能
Amazon AWS data Lake Work Pit 1
In depth understanding of JUC concurrency (I) what is JUC
2020-9-23 QT的定时器Qtimer类的使用。
IDEA公布全新默认UI,太清爽了(内含申请链接)
Flask-Migrate 检测不到db.string() 等长度变化
ctf-web之练习赛
数据科学【八】:SVD(一)
LeetCode 27. Removing Elements
eslint配置代码自动格式化
AtCoder Beginner Contest 253 F - Operations on a Matrix // 树状数组