Skip to content

Commit 8657bb6

Browse files
author
Your Name
committed
Palindrome using iterators
1 parent 8696129 commit 8657bb6

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
/**
5+
* Function to check if a string is palindrome.
6+
* @param s string to check.
7+
* @return true if the string is palindrome; false otherwise.
8+
*/
9+
bool checkPalindrome(std::string s) {
10+
auto front = s.cbegin(); // const_iterator pointing to the first character of the string
11+
auto back = s.cend() - 1; // const_iterator pointing to the last valid character of the string
12+
13+
// every time front moves forward, back moves back, both towards the center
14+
while(front != back) { // compare front and back
15+
if(*front != *back) { return false; }
16+
front++;
17+
back--;
18+
}
19+
20+
return true;
21+
}
22+
23+
/**
24+
* Driver code.
25+
*/
26+
int main() {
27+
std::string s;
28+
std::cin >> s;
29+
30+
if(checkPalindrome(s)) { std::cout << "Palindrome" << std::endl; }
31+
else { std::cout << "Not Palindrome" << std::endl; }
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)