Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define MAX 255
- struct data
- {
- bool is_mir;
- bool is_par;
- char str1[MAX];
- char str2[MAX];
- }simple;
- void judgement(char ch,int len1,int len2);
- int main()
- {
- int i,j;
- int len;
- char str[MAX];
- while(gets(str)!=NULL){
- len=strlen(str);
- for(i=len-1;i>=0;i--){
- judgement(str[i],len-1,i);
- }
- if(strcmp(simple.str1,str)==0) //去比對created的mirror string有無符合輸入字串,有則設is_mir為1否則為0
- simple.is_mir=1;
- else
- simple.is_mir=0;
- if(strcmp(simple.str2,str)==0) //去比對created的palindrome string有無符合輸入字串,有則設is_par為1否則為0
- simple.is_par=1;
- else
- simple.is_par=0;
- if(simple.is_mir==1&&simple.is_par==1)printf("%s -- is a mirrored palindrome.\n",str); //兩者皆符合則display『字串 -- is a mirrored palindrome.』
- else if(simple.is_mir==1&&simple.is_par==0)printf("%s -- is a mirrored string.\n",str); //符合鏡像則display『字串 -- is a mirrored string.』
- else if(simple.is_mir==0&&simple.is_par==1)printf("%s -- is a regular palindrome.\n",str); //符合迴文則display『字串 -- is a regular palindrome.』
- else printf("%s -- is not a palindrome.\n",str); //皆無則display 『字串 -- is not a palindrome.』
- printf("\n");
- simple.is_mir=0; //把bool的is_mir重新設回0
- simple.is_par=0; //把bool的is_par重新設回0
- memset(simple.str1,0,len); //string lib,把str1長度為len的內容都清空
- memset(simple.str2,0,len); //string lib,把str2長度為len的內容都清空
- }
- return 0;
- }
- void judgement(char ch,int len1,int len2)
- {
- if(ch=='A'||ch=='H'||ch=='I'||ch=='M'||ch=='O'||ch=='T'||ch=='U'||ch=='V'||ch=='W'||ch=='X'||ch=='Y'||ch=='1'||ch=='8')
- {
- simple.str1[len1-len2]=ch;
- }
- else if(ch=='E'||ch=='3')
- {
- if(ch=='E')
- simple.str1[len1-len2]='3';
- else
- simple.str1[len1-len2]='E';
- }
- else if(ch=='J'||ch=='L')
- {
- if(ch=='J')
- simple.str1[len1-len2]='L';
- else
- simple.str1[len1-len2]='J';
- }
- else if(ch=='S'||ch=='2')
- {
- if(ch=='S')
- simple.str1[len1-len2]='2';
- else
- simple.str1[len1-len2]='S';
- }
- else if(ch=='Z'||ch=='5')
- {
- if(ch=='Z')
- simple.str1[len1-len2]='5';
- else
- simple.str1[len1-len2]='Z';
- }
- else
- {
- simple.str1[len1-len2]=0; //記得設成空字元
- }
- simple.str2[len1-len2]=ch; //回文皆倒過來指派(如長度為9[0~8],其中index為8(end)的話回文要指派到0(start)來建立
- }
Add Comment
Please, Sign In to add comment