update palindrome.cpp
I improved the existing code so that it can now handle sentences with punctuation and spaces. This means that it now works for an edge case like "Sit on a potato pan, Otis"pull/937/head
parent
5d18a66cd8
commit
6b7a9560a2
|
@ -1,6 +1,7 @@
|
||||||
// Program to find whether a number and word is palindrome or not.
|
// Program to find whether a number and word is palindrome or not.
|
||||||
|
|
||||||
#include<iostream> //header file
|
#include<iostream> //header file
|
||||||
|
#include<ctype.h>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
int main() //Main function
|
int main() //Main function
|
||||||
{
|
{
|
||||||
|
@ -8,14 +9,34 @@ int main() //Main function
|
||||||
cin >> num_str; //taking input from user
|
cin >> num_str; //taking input from user
|
||||||
string new_str = ""; //define a new variable
|
string new_str = ""; //define a new variable
|
||||||
|
|
||||||
for(int x = (num_str.size()-1); x >= 0; x--){ //for loop started
|
|
||||||
new_str += num_str[x]; //assigning the value input by user to new variable in reverse order
|
string test_str = ""; //define new variables for cleaning the input
|
||||||
|
string low = "";
|
||||||
|
char temp;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for(i = 0; i < num_str.size(); i++){ //make num_str all lower case and remove all punctuation and spaces
|
||||||
|
temp = num_str[i];
|
||||||
|
if(isalnum(temp)){
|
||||||
|
low = char(tolower(temp)); //converting the character to be lower case
|
||||||
|
test_str.append(low); //adding that lowercase character to the new string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int x = (test_str.size()-1); x >= 0; x--){ //for loop started
|
||||||
|
new_str += test_str[x]; //assigning the value input by user to new variable in reverse order
|
||||||
} //for loop end
|
} //for loop end
|
||||||
|
|
||||||
cout << (num_str == new_str ? "palindrome" : "Non-palindrome"); //checking whether the value assigned to both variables is equal or not using ternary operator and printing whether it's palindrome or non-palindrome
|
string pal = "";
|
||||||
|
if(test_str == new_str){ //checking if the variables are equal and setting a string according to the result
|
||||||
|
pal = "palindrome";
|
||||||
|
}else{
|
||||||
|
pal = "Non-palindrome";
|
||||||
|
}
|
||||||
|
cout << pal; //printing if it is a palindrome or non-palindrome
|
||||||
|
|
||||||
return 0; //returning the main function
|
return 0; //returning the main function
|
||||||
}
|
}
|
||||||
|
|
||||||
//complexity of the program is O(n)
|
//complexity of the program is O(n)
|
||||||
//test cases:- 101,pop,asdfgfdsa,123454321,obobo,nancyiycnan etc.
|
//test cases:- 101,pop,asdfgfdsa,123454321,obobo,nancyiycnan etc.
|
||||||
|
//additional test cases: "Sit o,n a 'potat+o pan, Otis", "Sit on a potato pan, Otis"
|
||||||
|
|
Loading…
Reference in New Issue