回文串判定

回文串判定

Time Limit: 1000 ms  Memory Limit: 65536 KiB
Problem Description

输入一串字符(长度小于100),判断该串字符是否是回文串(正序读与逆序读内容相同)。

Input

输入一串字符(长度小于100)。

Output

若该串字符是回文串输出“yes",否则输出”no“。

Sample Input
asdfgfdsa
Sample Output
yes
Hint
Source

#include<bits/stdc++.h>
using namespace std;
int main(){
  char a[100];
  char b[100];
  gets(a);
  int j=0,i;
  for(i=strlen(a) - 1;i>=0;i--){


  b[j++]=a[i];
  }
  b[j]='\0';
   if(strcmp(a,b)==0)
        printf("yes\n");
   else
        printf("no\n");
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回文字符串是一个特殊的字符序列,即正序和倒序完全相同字符串。例如:"madam" 或者 "racecar"。在C语言中,我们经常处理字符串,并且需要一些方法来判断一个字符是否是回文。这通常涉及到对字符串进行逐字符的比较。 以下是一个C语言函数用于检测字符串是否为回文: ```c #include <stdio.h> #include <stdbool.h> bool isPalindrome(const char *str) { int len = strlen(str); for(int i = 0; i < len / 2; ++i) { // 跳过非字母数字字符,并将其转换为小写 while (!isalnum(str[i]) && i < len - 1) i++; while (!isalnum(str[len-i-1]) && len-i-1 > 0) len--; if(tolower(str[i]) != tolower(str[len-i-1])) { return false; } } return true; } int main() { const char* str1 = "Madam"; const char* str2 = "racecar"; const char* str3 = "hello"; printf("'%s' is palindrome? %s\n", str1, isPalindrome(str1) ? "Yes" : "No"); printf("'%s' is palindrome? %s\n", str2, isPalindrome(str2) ? "Yes" : "No"); printf("'%s' is palindrome? %s\n", str3, isPalindrome(str3) ? "Yes" : "No"); return 0; } ``` 在这个程序中,首先导入了必要的头文件。`isPalindrome()` 函数接收一个指向字符串的指针作为参数。该函数计算字符串的长度,并从两端开始逐步向中心移动,比较对应的字符。如果在任何时候发现两个对应位置的字符不匹配,则返回false,表示输入字符串不是一个回文。只有当所有的字符都匹配时,才会返回true。 运行此程序会显示以下结果: ``` 'Madam' is palindrome? Yes 'racescar' is palindrome? Yes 'hello' is palindrome? No ``` 以上是使用C语言判断字符串是否为回文的基本方法和代码实例。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值