回文串和镜像串问题

回文词(Palindromes, UVa401)
输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字0。所谓
回文串,就是反转以后和原串相同,如abba和madam。所有镜像串,就是左右镜像之后和原
串相同,如2S和3AIAE。注意,并不是每个字符在镜像之后都能得到一个合法字符。在本题
中,每个字符的镜像如图所示(空白项表示该字符镜像后不能得到一个合法字符)。
在这里插入图片描述
输入的每行包含一个字符串(保证只有上述字符。不含空白字符),判断它是否为回文
串和镜像串(共4种组合)。每组数据之后输出一个空行。
样例输入:
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
样例输出:
NOTAPALINDROME – is not a palindrome.
ISAPALINILAPASI – is a regular palindrome.
2A3MEAS – is a mirrored string.
ATOYOTA – is a mirrored palindrome.

#include<stdio.h>
#include<String.h>
char s1[] = "AEHIJLMOSTUVWXYZ12358";
char s2[] = "A3HILJMO2TUVWXY51SEZ8"; 

int f1(char s[])
{
	int i;
	for(i=0; i < strlen(s)/2; i++)
			if(s[i] != s[strlen(s)-i-1])
				break;
	if(i != strlen(s)/2) return 0;
	else return 1;
}
int f2(char s[])
{
	int i;
	for(i=0; i < strlen(s)/2; i++)
	{
		if(!strchr(s1,s[i])) break;//若s1中不含字符,即不属于镜像字符,直接中止 
		else
		{
			int j;
			for(j=0; j < strlen(s1)&&s1[j]!=s[i]; j++); //若是镜像字符,找到在数组中的位置 
			if(s[strlen(s)-1-i] != s2[j]) break;//如果对称字符不是原字符镜像,中止 
		}
	}
	if(i != strlen(s)/2) return 0;
	else return 1;
}
int main()
{
	char s[99];
	while(scanf("%s", &s) != NULL)
	{
		if(f1(s) && f2(s))
			printf("%s -- is a mirrored palindrome.\n", s);
		else if(f1(s) && !f2(s))
			printf("%s -- is a regular palindrome.\n", s);
		else if(!f1(s) && f2(s))
			printf("%s -- is a mirrored string.\n", s);
		else 
			printf("%s -- is not a palindrome.\n", s);
	}
	
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

复习你给的温柔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值