llf123456 发表于 2020-12-23 15:38:38

请大佬帮帮我

判断回文字符串 (20分)
本题要求编写函数,判断给定的一串字符是否为“回文”。所谓“回文”是指顺读和倒读都一样的字符串。如“XYZYX”和“xyzzyx”都是回文。

函数接口定义:
bool palindrome( char *s );
函数palindrome判断输入字符串char *s是否为回文。若是则返回true,否则返回false。

裁判测试程序样例:
#include <stdio.h>
#include <string.h>

#define MAXN 20
typedef enum {false, true} bool;

bool palindrome( char *s );

int main()
{
    char s;

    scanf("%s", s);
    if ( palindrome(s)==true )
      printf("Yes\n");
    else
      printf("No\n");
    printf("%s\n", s);

    return 0;
}

/* 你的代码将被嵌在这里 */
输入样例1:
thisistrueurtsisiht
输出样例1:
Yes
thisistrueurtsisiht
输入样例2:
thisisnottrue
输出样例2:
No
thisisnottrue

bool palindrome( char *s )
{
        int i,j,cnt=0,k=0;
        char ch,t;
        i=0;
        j=strlen(s)-1;
        for(k=0;k<strlen(s);k++)
        {
                ch = s;
        }
        while(i<j)
        {
                t=s;
                i++;
                j--;
                cnt++;
        }
       
        ch = '\0';
        t = '\0';
        if(strcmp(ch,t)==0)return 1;
        else return 0;
//我的这串代码哪里有问题

风过无痕1989 发表于 2020-12-23 16:16:49

你的代码在哪?

jackz007 发表于 2020-12-23 16:25:36

bool palindrome(char * s)
{
      bool r                  ;
      int i , m               ;
      for(r = true , m = strlen(s) , i = 0 ; i < m / 2 ; i ++) {
                if(s != s) {
                        r = false ;
                        break   ;
                }
      }
      return r                  ;
}

心驰神往 发表于 2020-12-23 16:34:07

   把while(i<j)
      {
                t=s;
                i++;
                j--;
                cnt++;
      }
中的t=s;换成t=s;试试
页: [1]
查看完整版本: 请大佬帮帮我