C++ C++;递归和指针问题
首先,这是一项任务 需要什么:C++ C++;递归和指针问题,c++,pointers,recursion,palindrome,C++,Pointers,Recursion,Palindrome,首先,这是一项任务 需要什么: 1.Take in string input 2. copy string (stripping out white space, punctuation, and covert all characters to uppercase in the process) 3. Then determine if this copied string is a palindrome. 确定回文所需的方法: Base Case: string length i
1.Take in string input
2. copy string (stripping out white space, punctuation, and
covert all characters to uppercase in the process)
3. Then determine if this copied string is a palindrome.
确定回文所需的方法:
Base Case: string length is <= 1
General Case: if first letter != last letter, false, otherwise
point to next letter and write '\0' to last letter and
call the method again
基本情况:字符串长度为四个错误
首先,您有三个案例,但您只希望执行一个,因此如果。。。否则如果。。。否则…
语句,而不是如果。。。如果。。。否则…
您拥有的语句
其次,您的比较是不正确的,因为您是在比较指针,而不是它们所指向的字符
第三个错误与第二个类似,当您试图缩短分配给指针而不是字符的字符串时
最后,您忘记将递归调用的结果分配给result
变量。这是一个很常见的新手错误
以下是我的努力(未经测试的代码)
bool isPalindrome(字符*副本)
{
布尔结果=假;
int size=字符数(拷贝);
char*last=©[size-1];
如果(大小复制!=最后一次
这些变量是指向char的指针。您需要在比较之前取消引用它们
尝试:
*复制!!**最后 < p>这不针对代码中的任何问题,也不显示如何根据赋值来编写代码。代码仅在此为完整性,说明如何用C++提供的工具解决问题,不需要重新创建轮子。
std::copy\u如果
和std::transform
是您的stringCopy
,那么带有向后和向前运算符的std::equal
基本上就是您在isAlindrome
中所做的。在这种情况下使用递归无论如何都不是一个好主意
#include <string>
#include <algorithm>
#include <iostream>
bool is_palindrom( const std::string &input ) {
std::string copy;
// copy everything except spaces and punctations using:
// copy_if, isspace and ispunct
std::copy_if(input.begin(), input.end(), std::back_inserter(copy), [] (int ch) -> bool {
return !::isspace(ch) && !::ispunct(ch);
});
// transform to uppercase
std::transform(copy.begin(), copy.end(), copy.begin(), ::toupper);
// check if palindrom using iterators and revers iterators
// copy.end() - halfway is valid as std::string::iterator is a random access iterator
size_t halfway = copy.size() / 2;
bool isPalindrom = std::equal(copy.begin(), copy.end() - halfway,
copy.rbegin(), copy.rend() - halfway);
return isPalindrom;
}
int main() {
std::cout << is_palindrom("2aba2") << std::endl; // 1
std::cout << is_palindrom("2 ab a2") << std::endl; // 1
std::cout << is_palindrom("2 abb a2") << std::endl; // 1
std::cout << is_palindrom("abca") << std::endl; // 0
}
#包括
#包括
#包括
bool是回文(const std::string和input){
std::字符串副本;
//使用以下方法复制除空格和穿孔之外的所有内容:
//复制\u if、isspace和ispunt
std::copy_if(input.begin()、input.end()、std::back_插入器(copy),[](int ch)->bool{
return!::isspace(ch)&&::ispunt(ch);
});
//转换为大写
std::transform(copy.begin(),copy.end(),copy.begin(),::toupper);
//检查回文是否使用迭代器和反向迭代器
//copy.end()-因为std::string::iterator是随机访问迭代器,所以有效
size\u t=copy.size()/2;
bool isPalindrom=std::equal(copy.begin(),copy.end()-middle,
copy.rbegin(),copy.rend();
返回isPalindrom;
}
int main(){
std::cout而不是result=true;
调用isAlindrome(copy);
它的返回值不做任何事情。它返回的值不重要吗?嗯。我在main()中测试函数的返回值是真是假。但您不能只调用一次isPalindrome()
。因此,您需要处理多个返回值。函数中返回的值isPalindrome()
用于减小大小的字符串。值isPalindrome()
在main()中返回是针对整个字符串的。我明白了,是的,你是对的。我误解了。代码现在工作得很好。我感谢反馈。哇,我甚至没有想到从递归调用中捕获返回值。现在完全有意义了。谢谢!这完全解决了我的问题。解释也很棒!
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int charCount(char * copy)
{
int count = 0;
for (int i = 0; copy[i] != '\0'; i++)
{
count++;
}
return count;
}
bool isPalindrome(char *copy)
{
bool result = false;
int size = charCount(copy);
char * last = ©[size - 1];
if (size <= 1)
{
result = true;
}
if (copy != last)
{
result = false;
}
else
{
++copy;
last = '\0';
isPalindrome(copy);
}
return result;
}
void stringCopy(char * source, char * destination)
{
int sourceIndex = 0;
int destIndex = 0;
while (source[sourceIndex] != '\0')
{
while (!(isalnum(source[sourceIndex])) && source[sourceIndex] != '\0')
{
sourceIndex++;
}
if (source[sourceIndex] == '\0')
{
break;
}
if (isalpha(source[sourceIndex]))
{
destination[destIndex] = toupper(source[sourceIndex]);
}
if (isdigit(source[sourceIndex]))
{
destination[destIndex] = source[sourceIndex];
}
sourceIndex++;
destIndex++;
}
destination[destIndex] = '\0';
}
int main()
{
string input = "";
cout << "Enter a string: ";
getline(cin, input);
char * source = &input[0];
int sourceSize = charCount(source);
char * copy = new char[sourceSize];
stringCopy(source, copy);
int copySize = charCount(copy);
if (isPalindrome(copy))
{
cout << input << " is a palindrome!" << endl;
}
else
{
cout << input << " is not a palindrome" << endl;
}
return 0;
}
bool isPalindrome(char *copy)
{
bool result = false;
int size = charCount(copy);
char * last = ©[size - 1];
if (size <= 1)
{
result = true;
}
else if (*copy != *last) // else if and *copy != *last, not copy != last
{
result = false;
}
else
{
++copy;
*last = '\0'; // *last not last
result = isPalindrome(copy); // capture return value from recursive call
}
return result;
}
#include <string>
#include <algorithm>
#include <iostream>
bool is_palindrom( const std::string &input ) {
std::string copy;
// copy everything except spaces and punctations using:
// copy_if, isspace and ispunct
std::copy_if(input.begin(), input.end(), std::back_inserter(copy), [] (int ch) -> bool {
return !::isspace(ch) && !::ispunct(ch);
});
// transform to uppercase
std::transform(copy.begin(), copy.end(), copy.begin(), ::toupper);
// check if palindrom using iterators and revers iterators
// copy.end() - halfway is valid as std::string::iterator is a random access iterator
size_t halfway = copy.size() / 2;
bool isPalindrom = std::equal(copy.begin(), copy.end() - halfway,
copy.rbegin(), copy.rend() - halfway);
return isPalindrom;
}
int main() {
std::cout << is_palindrom("2aba2") << std::endl; // 1
std::cout << is_palindrom("2 ab a2") << std::endl; // 1
std::cout << is_palindrom("2 abb a2") << std::endl; // 1
std::cout << is_palindrom("abca") << std::endl; // 0
}