递归函数-输出不正确
Recursion functions - Output is incorrect
我正在创建一个程序,该程序使用递归函数来计算句子中的元音,并确定它是否是回文。我遇到的问题是,它说输入的句子不是回文,即使它是。任何帮助都将不胜感激。非常感谢。
#include<iostream>
#include <cmath>
using namespace std;
struct Sentence
{
int CountVowels(string , int);
public:
Sentence (string);
bool isPal(string , int);
void Print();
string s;
int numVowel;
int length;
//~Sentence();
};
Sentence :: Sentence (string b)
{
s = b;
length = 0;
numVowel = CountVowels(s, 0);
}
int Sentence :: CountVowels(string myWord, int startindex)
{
length ++;
int pandi;
if(myWord[startindex])
{
if (myWord[startindex] != 'a' && myWord[startindex] != 'e' && myWord[startindex] != 'i' && myWord[startindex] != 'o' && myWord[startindex] != 'u')
{
pandi = 0;
}
else pandi = 1;
return pandi + CountVowels(myWord, startindex + 1);
}
return 0;
}
bool Sentence :: isPal(string myWord, int size)
{
int r = myWord.size() - size;
int t = size - 1;
if (size == r || r == t)
return true;
if ((myWord[r]) != (myWord[t]))
return false;
return isPal(myWord, -- size);
}
void Sentence :: Print()
{
cout << s [-- length];
if (length == 0)
{
cout << endl;
return;
}
Print ();
}
/*Sentence :: ~Sentence()
{
cout << "ntilde deletenn";
}*/
int main ()
{
string userW;
cout << "Enter a sentence: n";
getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
Sentence userSent(userW);
cout << "The number of vowels in the sentence is " << userSent.numVowel << endl;
cout << "" << endl;
cout << "The sentence " << userSent.s << " is" <<
(userSent.isPal(userSent.s, userSent.s.size()) ? " Palindromen" : " Not Palindromen");
return 0;
}
更新:我现在正在尝试删除特殊字符。所以它看起来像这个
string userW;
cout << "Enter a sentence: n";
getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
但我得到了这个错误:
In function 'int main()':
88:85: error: 'remove_if' was not declared in this scope
我已经审阅了您的程序。您正在尝试输出函数pint()中的字符串。问题是,当你使用时
cout << "The sentence backwards is: " << userSent.Print();
但是函数Print()没有任何返回类型。(因为这是无效类型)。这里你应该使用
cout << "The sentence backwards is: " ;
userSent.Print();
现在它起作用了。
您的代码很好,只是它没有从句子中删除逗号、空格和任何非字母的东西。此外,您还需要对字符进行不区分大小写的比较。这是必需的,否则示例
一个人,一个计划,一条运河,巴拿马
甜点,我强调
不会是栅栏。
要从用户输入中删除特殊字符,可以使用lambda
string userW; cout << "Enter a sentence: n"; getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
EDIT您还可以尝试以下操作来避免对lambda:的需要
userW.erase(std::copy_if(userW.begin(), userW.end(), userW.begin(), isalpha), userW.end());
要进行不区分大小写的比较,在函数isPal中,您可以更改以下内容:
if((myWord[r])!=(myWord[t])
进入这个:
if (tolower(myWord[r]) != tolower(myWord[t]))
相关文章:
- 不正确的比较和交换计数器输出用于快速排序功能
- Windows 10命令提示符的输出不正确
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- C++ - 为什么 unicode 输出不正确?
- 连接的组件程序产生不正确的输出
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 我的叉子输出不正确
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 堆排序,我无法弄清楚我的代码出了什么问题,输出顺序不正确
- C++使用 CEIL 和长度输出不正确的值
- 使用 iconv 进行 UTF8 转换的输出不正确
- C strftime()输出不正确
- 不正确的输出和变量未用Eclipse CDT初始化构造函数
- 当行数和列不匹配时,输出不正确
- 合并排序不正确的输出 c++
- 从堆栈中读取字符后,如何修复不正确的文件输出
- C++ 嵌套循环输出不正确
- 矩阵和矢量乘法,输出不正确的产品
- 我对这个问题有问题,我的输出不正确
- 测试整数是否在范围内时,逻辑错误和不正确输出