Problem Description
source:https://uva.onlinejudge.org/external/109/10945.html
Unforunately for our lazy “heroes”, the nuts were planted by an evil bear known as.. Dave, and they’ve fallen right into his trap. Dave is not just any bear, he’s a talking bear, but he can only understand sentences that are palindromes. While Larry was dazed and confused, Ryan figured this out, but need a way to make sure his sentences are palindromic. So he pulled out his trustly iPod, which thankfully have this program you wrote just for this purpose... or did you?
Input
You’ll be given many sentences. You have to determine if they are palindromes or not, ignoring case and punctuations. Every sentence will only contain the letters A-Z, a-z, ‘.’, ‘,’, ‘!’, ‘?’. The end of input will be a line containing the word ‘DONE’, which should not be processed.
Output
On each input, output ‘You won’t be eaten!’ if it is a palindrome, and ‘Uh oh..’ if it is not a palindrome.
Sample Input
Madam, Im adam!
Roma tibi subito motibus ibit amor.
Me so hungry!
Si nummi immunis
DONE
Sample Output
You won’t be eaten!
You won’t be eaten!
Uh oh..
You won’t be eaten!
Solution:
source:https://uva.onlinejudge.org/external/109/10945.html
Unforunately for our lazy “heroes”, the nuts were planted by an evil bear known as.. Dave, and they’ve fallen right into his trap. Dave is not just any bear, he’s a talking bear, but he can only understand sentences that are palindromes. While Larry was dazed and confused, Ryan figured this out, but need a way to make sure his sentences are palindromic. So he pulled out his trustly iPod, which thankfully have this program you wrote just for this purpose... or did you?
Input
You’ll be given many sentences. You have to determine if they are palindromes or not, ignoring case and punctuations. Every sentence will only contain the letters A-Z, a-z, ‘.’, ‘,’, ‘!’, ‘?’. The end of input will be a line containing the word ‘DONE’, which should not be processed.
Output
On each input, output ‘You won’t be eaten!’ if it is a palindrome, and ‘Uh oh..’ if it is not a palindrome.
Sample Input
Madam, Im adam!
Roma tibi subito motibus ibit amor.
Me so hungry!
Si nummi immunis
DONE
Sample Output
You won’t be eaten!
You won’t be eaten!
Uh oh..
You won’t be eaten!
Solution:
#include <algorithm> #include <cstdio> #include <cmath> #include <cstring> #include <deque> #include <fstream> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include<stdio.h> #include<stdlib.h> using namespace std; int main() { int i,len,count,j,k,len1; char ch[1000],input[1000],revers[1000]; memset(input,'\0',sizeof(input)); memset(revers,'\0',sizeof(revers)); while(gets(ch)) { if(strcmp(ch,"DONE")==0) break; len=strlen(ch); count=0; for(i=0;i<len;i++) { if(ch[i]>=65 && ch[i]<=90) input[count++]=ch[i]+32; else if(ch[i]>=97 && ch[i]<=122) input[count++]=ch[i]; } len1=strlen(input); k=0; for(j = len1-1; j >-1; j--) revers[k++] = input[j]; if(strcmp(input,revers)==0) printf("You won't be eaten!\n"); else printf("Uh oh..\n"); memset(ch,'\0',sizeof(ch)); memset(input,'\0',sizeof(input)); memset(revers,'\0',sizeof(revers)); } return 0; }
No comments:
Post a Comment
Write your comment - Share Knowledge and Experience