栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

自学C语言-视频-张鹏伟-20211224

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自学C语言-视频-张鹏伟-20211224

检测两个字符串是近左旋相同还是近右旋相同

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#define LEN 33
char *my_fgets(char *str,const int n);
int compare_str(const char *str1,const char *str2);

int main(){
	char str1[LEN]={0},str2[LEN]={0};
	while(1){
		printf("Input the first string(limit:%d):",LEN-1);
		if(!my_fgets(str1,LEN))
			exit(0);
		printf("Input the second string(limit:%d):",LEN-1);
		if(!my_fgets(str2,LEN))
			exit(0);
		int result=compare_str(str1,str2);
		switch(result){
		case 0:printf("字符串"%s"与"%s"毫不相关。n",str1,str2);break;
		case -1:printf("字符串"%s"与"%s"完全相同。n",str1,str2);break;
		case 3:printf("字符串"%s"与"%s"近左旋与近右旋皆相同。n",str1,str2);break;
		case 1:printf("字符串"%s"与"%s"是近左旋相同。n",str1,str2);break;
		case 2:printf("字符串"%s"与"%s"是近右旋相同。n",str1,str2);break;
		default:fputs("无法判断。n",stdout);break;
		}
	}
return 0;}
//
char *my_fgets(char *str,const int n){
	char *gets,*find;
	if((gets=fgets(str,n,stdin))&&*gets!=10)
		if(find=strchr(str,10))
			*find=0;
		else
			while(getchar()!=10);
	else
		return 0;
return gets;}

int compare_str(const char *str1,const char *str2){
	int len1=strlen(str1),len2=strlen(str2);
	if(len1!=len2)
		return 0;
	if(!strncmp(str1,str2,len1))
		return -1;
	char *str_copy=(char *)calloc(len1+1,sizeof(char));
	char temp[2]={0};
	int direction=0;
	for(int i=0,j=len2-1;i!=j;i++,j--){
		if(str1[0]==str2[j])
			direction=1;
		else if(str1[len1-1]==str2[i])
			direction=2;
		if(direction&&abs(i-j)<=1)
			direction=3;
		if(direction)
			break;
	}
	strncpy(str_copy,direction==1?str1:str2,len1);
	for(int i=0;i 

另外有一种方法是追加复制自身

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/690024.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号