Vous êtes sur la page 1sur 7

#include <iostream.h> #include <conio.h> #include <string.

h> int main() { char strn[80]; int i,j,flag=0,len; cout<<"Enter the string:"; cin.getline(strn,80); len=strlen(strn); for(i=0,j=len-1;i<=(len/2);++i,--j) { if(strn[i]==strn[j]) flag=1; } if(flag) cout<<"Palindrome"; else cout<<"Not a palindrome"; getch(); }

#include <iostream> #include <string> using namespace std; void main() { char phrase[85]; bool palindrome; int length; int count = 0; cout << "Enter a phrase: "; cin.getline (phrase, 84); length = strlen (phrase); for (count = 0; count != length / 2.0; count++) { int end = length - count; if (phrase[count] == phrase [end]) { cout << "This is a palindrome!"; } else { break; } } }

#include //iostream.h

#include //conio.h void copy(char a[],char b[]) { for(int i=0;a[i]!='\0';i++) { b[i]=a[i]; } b[i]='\0'; } int strlength(char *c1) { for(int i=0;*(c1++)!='\0';i++); return i; } void reverse(char c[]) { char t; int l=strlength(c); for(int i=0;i<=l/2;i++) { t=c[l-1]; c[l-1]=c[i]; c[i]=t; l--; } } void palindrome(char ch[]) { char chb[20];

copy(ch,chb); int l=strlength(ch); reverse(ch); int flag=0; for(int i=0;i<l;i++) { if(chb[i]!=ch[i]) { flag=1; break; } flag=0; } if(flag==0) cout<<"\nTHE STRING IS A PALINDROME"; else cout<<"\nTHE STRING IS NOT A PALINDROME"; } void main() { char ch[20]; int length; clrscr(); cout<<"Enter a String:"; cin>>ch; length=strlength(ch); cout<<"Length="<<length; getch(); palindrome(ch); getch(); }

#include <iostream> #include <string> using namespace std; int main() { string word; bool palindrome; cout << "Enter a word: "; cin >> word; for (int x=0; x < word.length()-1; x++) { if (tolower(word[x]) != tolower(word[word.length()-(x+1)])) { palindrome = false; break; } else palindrome = true; } if (palindrome) cout << "It is a palindrome"; else cout << "It is a not palindrome"; return 0; }

#include <iostream.h> 03 #include <lvpString.h><p> 04 void reverse(String string1) 05 {</p><p>for (int letter = string1.length()-1; letter>=0; letter--) 06 cout << string1[letter]; 07 }</p><p> 08 bool palindrome(String string1) 09 { 10 if (string1==reverse(string1)) 11 return(true); 12 else 13 return(false); 14 } </p><p></p><p>int main() 15 { 16 String string1; 17 cout<<"Enter a string:"; 18 cin>>string1; 19 if (palindrome(string1)==true) 20 cout<<"The word is a palindrome."<<endl; 21 else 22 cout<<"The word is not a palindrome."<<endl; 23 return(0); 24 } 25 26 </p>

#include <iostream.h> 03 #include <lvp/String.h><p> 04 String reverse(String &string1) 05 { 06 String string2; 07 for (int letter = string1.length()-1; letter>=0; letter--) 08 string2 = string1[letter]; 09 return (string2); 10 }</p><p> 11 bool palindrome(String string1) 12 { 13 if (string1==reverse(string1)) 14 return(true); 15 else 16 return(false); 17 }</p><p></p><p>int main() 18 { 19 String string1; 20 cout<<"Enter a string: "; 21 cin>>string1; 22 if (palindrome(string1)==true) 23 cout<<"The word is a palindrome."<<endl; 24 else 25 cout<<"The word is not a palindrome."<<endl; 26 return(0); 27 } 28 29 </p>

#include <iostream> #include<conio.h> #include <string> using namespace std; int main() { char str[100]; bool flag; cout<<"Enter word :"; cin>>str; int x=strlen(str)-1; for(int i=0;i<=x;i++) { if(str[i]==str[x-i]) { flag=true; continue; } else { flag=false; break; } } if(flag==true) cout<<"Palindrome"<<endl; else cout<<"Not a palindrome"<<endl; getch(); }

#include <iostream> using namespace std; int clean(char *, int); bool isPalindrome(char *, int, int); bool isQuit(char *); onst int stringSize = 255; nt main() { char toCheck[stringSize]; char origStr[stringSize]; do { cout << "Enter palindrome: "; cin.getline(origStr, sizeof(toCheck)); for(int i = 0; i < stringSize; i++) { toCheck[i] = origStr[i]; } int newSize = clean(toCheck, strlen(toCheck)); char newToCheck[newSize]; for(int n = 0; n < newSize; n++) { newToCheck[n] = toCheck[n]; } if(isQuit(newToCheck)) { return false; } if(isPalindrome(newToCheck, 0, newSize - 1)) { cout << "Yes, " << origStr << " is a palindrome!\r\n"; } else { cout << "No, " << origStr << " is not a palindrome!\r\n"; } } while(true); system("PAUSE"); return 0; } ool isQuit(char* phrase) { char quit[6] = "~quit"; // Compare the char array to '~quit'

for(int i = 0; i < 5; i++) { if(phrase[i] != quit[i]) { return false; } } return true; } int clean(char* phrase, int size) { int currChar = 0; // Loop through the original array for(int i = 0; i < size; i++) { if(((int)phrase[i] >= 48 && (int)phrase[i] <= 57) || ((int)phrase[i] >= 65 && (int)phrase[i] <= 90) || ((int)phrase[i] >= 97 && (int)phrase[i] <= 122)) { phrase[currChar] = tolower(phrase[i]); currChar++; } } return currChar; } bool isPalindrome(char* phrase, int start, int end) { if(phrase[start] == phrase[end]) { if(start == end || start + 1 == end) { return true; } else { return isPalindrome(phrase, start + 1, end 1); } } else { return false; }
}

Vous aimerez peut-être aussi