Almg
-
C++,
pasted
on Jan 18:
|
//Program that determines if a specific word or sentence is palindrome
#include <iostream> //import libraries
#include <string>
using namespace std;
//constant value
const int N=520;
//main program
int main () {
char sentence[N+1] ;
bool ispal=true; //false if the sentence is not palindrome
int i=0, n; //The sentence is traversed with an index
cout << "Enter the sentence finished on point : racecar, amore, roma, name not the man, race fast, safe car, hello world";
cin>> sentence [i];
while (sentence [i] !='.') cin>> sentence[i++];
//use the function strlen for ignore spaces
sentence [i]='\0';
n=strlen(sentence);
//Search if the sentence is palindrome or not.
i=0;
while ((i<n/2) and (ispal))
{
if (sentence [i] != sentence [n-i-1]) ispal=false;
else i++;
}
if (ispal)
cout<<"true, is palindrome." ;
else
cout<< "false." ;
return 0;
}
|
Output:
|
Enter the sentence finished on point : racecar, amore, roma, name not the man, race fast, safe car, hello world
Segmentation fault
|
|