- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的任务是编写一个递归函数来确定用户输入的字符串是否为回文。我认为我有一个非常强大的代码基础来解决这个问题,但它似乎并没有像我想要的那样工作。
我正在做一些故障排除以找出问题所在,看起来如果我的字符串 <= 8 个字符长,数组的长度出于某种原因自动变为 14。可以使用比我更擅长 C++ 的人的一些故障排除和输入。
这是我的代码:
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
bool isPalindrome(char[], int, int);
int main()
{
char palin[100],
lowerPalin[100];
cout << "Enter a line that might be a palindrome:" << endl;
cin.get(palin, 100);
for (int i = 0, x = 0; i < strlen(palin); i++) // this for loop will remove punctuation, white space and make it all lowercase
{
if((palin[i] != ' ') && (ispunct(palin[i]) == false))
{
lowerPalin[x] = tolower(palin[i]); // transfering inputted array into new array with just alpha characters
x++;
}
}
int low = 0,
high = strlen(lowerPalin);
if (isPalindrome(lowerPalin, low, high))
cout << "The string is a palindrome." << endl;
else
cout << "The string is NOT a palindrome." << endl;
return 0;
}
bool isPalindrome(char lowerPalin[], int low, int high)
{
if (lowerPalin[low] == lowerPalin[high])
return isPalindrome(lowerPalin, low + 1, high - 1);
if (lowerPalin[low] != lowerPalin[high])
return false;
return true;
}
我还在努力学习递归,所以如果我的底部函数有任何问题,请告诉我。
最佳答案
if anything is wrong with my bottom function please let me know.
high
,您需要终止递归低于
low
.
lowerPalin
上越界。
lowerPalin
从
palin
填充后.否则,
strlen
将寻找那个
0
在未初始化的数组中;它可能会在位置
14
找到它(发生在你身上)或根本没有。这可能会导致访问冲突异常!
关于c++ - 编写一个程序,读取一串字符并调用递归函数来确定字符串中的字母是否构成回文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69110725/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!