forked from deutranium/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome_WithIterators.cpp
More file actions
34 lines (28 loc) · 899 Bytes
/
palindrome_WithIterators.cpp
File metadata and controls
34 lines (28 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
/**
* Function to check if a string is palindrome.
* @param s string to check.
* @return true if the string is palindrome; false otherwise.
*/
bool checkPalindrome(std::string s) {
auto front = s.cbegin(); // const_iterator pointing to the first character of the string
auto back = s.cend() - 1; // const_iterator pointing to the last valid character of the string
// every time front moves forward, back moves back, both towards the center
while(front != back) { // compare front and back
if(*front != *back) { return false; }
front++;
back--;
}
return true;
}
/**
* Driver code.
*/
int main() {
std::string s;
std::cin >> s;
if(checkPalindrome(s)) { std::cout << "Palindrome" << std::endl; }
else { std::cout << "Not Palindrome" << std::endl; }
return 0;
}