-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.h
More file actions
51 lines (45 loc) · 1.35 KB
/
Copy pathpalindrome.h
File metadata and controls
51 lines (45 loc) · 1.35 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef CPP_ALGORITHM_PALINDROME_H
#define CPP_ALGORITHM_PALINDROME_H
#include <string>
namespace Palindrome
{
/**
* \brief Check if a string is palindromic.
* Use one pointer to iterate half of the string.
* \param str input string
* \return true if the string is palindromic, false otherwise
*/
bool IsPalindromic1(const std::string& str);
/**
* \brief Check if a string is palindromic.
* Use two pointers to iterate the string. One pointer starts from the beginning, the other starts from the end.
* \param str input string
* \return true if the string is palindromic, false otherwise
*/
bool IsPalindromic2(const std::string& str);
}
// ----------------------------------------------------------------------------
inline bool Palindrome::IsPalindromic1(const std::string& str)
{
for (int i = 0; i < static_cast<int>(str.length()) / 2; ++i)
{
if (str[i] != str[str.length() - 1 - i])
{
return false;
}
}
return true;
}
// ----------------------------------------------------------------------------
inline bool Palindrome::IsPalindromic2(const std::string& str)
{
for (int i = 0, j = static_cast<int>(str.length()) - 1; i < j; ++i, --j)
{
if (str[i] != str[j])
{
return false;
}
}
return true;
}
#endif