写一函数实现对输入字符串是否回文的判断,备注:回文即顺序读和倒序读均是一样的字符串 ——2.0版(其他方法见1.0 3.0)

//思路:先把字符串完全倒过来 用字符串比对函数,再比较前后两字符串是否相等;
#include<stdio.h>
#include<string.h>//strlen
#include<stdlib.h>//malloc
void stringtest(char *src)
{
   // char *src="hello.world";
    char *dest=NULL;
    int len=strlen(src);//len = 11
    dest=(char *)malloc(len+1);//
    printf("%d\n",len );
    char *d=dest;
    char *s=&src[len-1];//指针下标法 src[0];src[1];src[2]...src[len-1]
    while(len-- != 0)
    {
        *d++=*s--;// *d = *s; d--;s--
    }
    *d = '\0';
    printf( "%s\n",dest);
    int length =strlen(dest);
    printf("%d\n",length/2 );
    int num = strncmp(dest,src,length/2);
    free(dest);//malloc();free()
    if( num == 0)
    {
        printf("this string is a palindrome\n");
    }else
    {
        printf("this string is not a palindrome\n");
    }
}
int main() {
    char p[128];
    gets(p);
    stringtest(p);
    return 0;
}

结果:/eg1

aba
3
aba
3
this string is a palindrome

eg2

abcdefg
7
gfedcba
7
this string is not a palindrome

思路来源

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sugcoffee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值