当前位置:网站首页>【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
【每日一题】写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
2022-07-02 06:12:00 【谢西山】
写一个函数,判断一个字符串是否为另外一个字符串旋转之后的字符串。
例如:给定s1 =AABCD和s2 = BCDAA,返回1
给定s1=abcd和s2=ACBD,返回0.
AABCD左旋一个字符得到ABCDA
AABCD左旋两个字符得到BCDAA
AABCD右旋一个字符得到DAABC
方案一
分析:s1旋转一次为ABCDA,和s2比较,调用strcmp,返回值等于0 的值,则说明是s2是s1旋转得到的,否则继续在ABCDA的基础上再旋转一次,得到BCDAA,和s2比较,调用strcmp,返回值等于0 的值,则说明是s2是s1旋转得到的,继续循环,直到循环5次,或着在循环中碰到正好s1=s2,那么说明,s2是s1旋转得到的。
# define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<string.h>
int judge(char* s1, char* s2,int len)
{
//先旋转一个字符
for (int i = 1; i <= len; ++i)
{
//i为1时第一次旋转
char tem = *s1;
int j=0;
for (j = 0; j < len; ++j)
{
*(s1 + j) = *(s1 + j + 1);
}
*(s1+j) = tem;//将刚才取出的值放到最后一个元素
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;
}
方案2
使用函数strcat组成新的字符串s3,AABCDAABCD,用s2来遍历比较函数是否为s1的子函数,若是则s2是s1旋转得到的。
strncat 函数
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))//如果两个字符串长度不相等,那么不用比较都直到不是旋转所得。
return 0;
strncat(arr1, arr1, strlen(arr1));//append a string
return strstr(arr1, arr2) != NULL;//查找子字符串,找到则返回值为非0
}
int main()
{
char arr1[20] = "AABCD";
char arr2[10] = "BCDAA";
if (FlipCheck(arr1, arr2))
printf("Find it");
else
printf("NO Find");
return 0;
}
我发了讲解视频,在评论区,自己去看
今日垃圾话:那个公司又叫我了,啊啊啊
边栏推荐
- Memcached installation
- Browser principle mind map
- Bgp Routing preference Rules and notice Principles
- 来自读者们的 I/O 观后感|有奖征集获奖名单
- Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
- Ros2 --- lifecycle node summary
- sudo提权
- Support new and old imperial CMS collection and warehousing tutorials
- Amazon AWS data Lake Work Pit 1
猜你喜欢

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?

Jetpack Compose 与 Material You 常见问题解答

深入学习JVM底层(二):HotSpot虚拟机对象

51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)

500. Keyboard line

日志(常用的日志框架)

官方零基础入门 Jetpack Compose 的中文课程来啦!

Comment utiliser mitmproxy

深入了解JUC并发(一)什么是JUC

网络相关知识(硬件工程师)
随机推荐
深入学习JVM底层(三):垃圾回收器与内存分配策略
Shenji Bailian 3.54-dichotomy of dyeing judgment
经典文献阅读之--SuMa++
Verifying downloaded files using sha256 files
递归(迷宫问题、8皇后问题)
BGP报文详细解释
来自读者们的 I/O 观后感|有奖征集获奖名单
Current situation analysis of Devops and noops
Cglib代理-代码增强测试
LeetCode 90. 子集 II
Use some common functions of hbuilderx
Don't use the new WP collection. Don't use WordPress collection without update
标签属性disabled selected checked等布尔类型赋值不生效?
Page printing plug-in print js
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
Database learning summary 5
Zhuanzhuanben - LAN construction - Notes
注解和反射详解以及运用
一起学习SQL中各种join以及它们的区别
Cookie plugin and localforce offline storage plugin