首页 > 试题广场 >

判断两个字符串是否互为逆置

[编程题]判断两个字符串是否互为逆置
  • 热度指数:2242 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
判断两个字符串是否互为旋转,即能否逆置一个字符串得到另外一个字符串。 例如:给定str1= "HUANJU" 和 str2 = "UJNAUH",返回1, 给定str1 = "HUANJU" 和 str2 = "ACBD",返回0. 函数原型:int isRevertStr(const char *str1,const char *str2);

输入描述:
输入为一行,两个字符串str1和str2,以空格分隔.


输出描述:
如果两个字符串是互为逆置输出1,否则输出0
示例1

输入

ABCD ABCD

输出

0
#include <stdio.h>
#include<string.h>
int isRevertStr(const char*str1,const char*str2){
    int len=strlen(str1);
    for(int i=len-1, j=0;i>=0;i--,j++){

        if(str1[i]!=str2[j])
        return 0;
    }
    return 1;
}
int main() {
  int arr1[20]={0};
  int arr2[20]={0};
scanf("%s",arr1);
scanf("%s",arr2);
int ret=isRevertStr(arr1,arr2);
printf("%d",ret);
    return 0;
}
发表于 2023-08-13 22:11:42 回复(0)
#include <stdio.h>
#include <string.h>
int main()
{
    char buf1[100],buf2[100],temp[100],tp;
    int i,len;
    scanf("%s",buf1);
    scanf("%s",buf2);
    strcpy(temp, buf2);
    len=strlen(temp);
    for(i=0;i<len/2;i++)
    {
        tp=temp[i];
        temp[i]=temp[len-i-1];
        temp[len-i-1]=tp;
    }
    if(strcmp(buf1, temp)==0)
    {
        printf("1\n");
    }
    else
    {
        printf("0\n");
    }
}
发表于 2023-04-19 21:23:36 回复(0)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char a[100], b[100], c, temp;
    int i = 0, m = 0;
    while (scanf("%c", &c) && c != ' ')
    {
        a[i] = c;
        i++;
    }
    gets(b);
#if 0
    puts(a);
    puts(b);
    system("pause");
#endif
    i = strlen(b) - 1;
    while (m < i)
    {
        temp = b[m];
        b[m] = b[i];
        b[i] = temp;
        m++;
        i--;
    }
#if 0
    puts(a);
    puts(b);
    system("pause");
#endif
    if (strcmp(a, b) == 0)
    {
        printf("1");
        return 0;
    }
    else
    {
        printf("0");
        return 0;
    }
}

发表于 2022-07-22 15:30:35 回复(0)