gpt4 book ai didi

c - 为什么我无法在C语言中使用for循环比较两个字符串[如果字符串彼此相反]

转载 作者:行者123 更新时间:2023-11-30 20:14:45 25 4
gpt4 key购买 nike

#include<stdio.h>
#include<string.h>

int main()
{
char string1[100],string2[100];
int count,i=0,j=0;

gets(string1);
gets(string2);

for(i=0;i<strlen(string1)-1;i++)
{
for(j=strlen(string2)-1;j<0;j--)
{
if(string1[i]==string2[j])
{
printf("They are reverse of each other");
}
else
printf("They are not");
}
}

}

/* 我试图通过检查 string1 的第一个索引和 string2 的最后一个索引并分别递增和递减来检查 ABC 和 CBA 是否相等。 */

最佳答案

试试这个代码,它用于检查两个字符串是否彼此相反。

#include<stdio.h>
#include<string.h>

int main()
{
char s1[100],s2[100];
int count,i=0,j=0, flag=1;
gets(s1);
gets(s2);
int l1=strlen(s1), l2=strlen(s2);
if(l1==l2)
{
l2--;
for(i=0;i<l1;i++)
{
if(s1[i]!=s2[l2-i])
{
flag=0;
break;
}
}
if(flag)
printf("Both are reverse to each other\n");
else
printf("Not revrese to each other\n");
}
else
printf("Not revrese to each other\n");
}

关于c - 为什么我无法在C语言中使用for循环比较两个字符串[如果字符串彼此相反],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24764567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com