Given an integer with an arbitrary number of numerical digits, write a function to check iteratively if the integer is a palindrome, e.g., 5679765.且要使用 using namespace std;
时间: 2024-02-01 16:04:36 浏览: 23
Sure! Here's an example function that checks if an integer is a palindrome:
```c++
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(int num) {
string str = to_string(num);
int start = 0, end = str.length() - 1;
while (start < end) {
if (str[start] != str[end]) {
return false;
}
start++;
end--;
}
return true;
}
int main() {
int num = 5679765;
if (isPalindrome(num)) {
cout << num << " is a palindrome" << endl;
} else {
cout << num << " is not a palindrome" << endl;
}
return 0;
}
```
The function `isPalindrome` converts the integer to a string, and then iteratively checks if the characters at the start and end of the string match. If they don't match, the function returns `false`. If the function completes the loop without finding any mismatches, it returns `true`.
Hope this helps!
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)