gpt4 book ai didi

c++ - 递归函数 - 删除特殊字符

转载 作者:行者123 更新时间:2023-11-30 02:35:26 26 4
gpt4 key购买 nike

我需要一个函数来删除字符串中的特殊字符。我正在创建一个程序,它接受一个句子并计算元音并确定它是否是回文。但如果有的话,我需要删除特殊字符。我使用的是 lambda,但它与我使用的编译器不兼容,而这正是我的教授希望我们的程序编译的地方。因此,如果有人有我可以使用的其他功能,我将非常感激。这些是我得到的错误: 错误:预期的主表达式在 â[â 标记之前 错误:预期在 â]â 标记之前的主表达式 错误:期望主表达式在 âcharâ 之前我评论了上面错误所在的行。

 #include<iostream> 
#include <cmath>
#include <algorithm>


using namespace std;

//Create a structure called Sentence
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);
}

//Count Vowels function using recursion
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;
}

// Check if it palindorme using recursion
bool Sentence :: isPal(string myWord, int size)
{
int r = myWord.size() - size;
int t = size - 1;

//size = r will be true whenn the size of the string is even and the 2 middle characters have been checked
if (size == r || r == t)

return true;
//r = t will be true when the size of the string is odd and the two characters on either side of the middle character have been checked



if (tolower(myWord[r]) != tolower(myWord[t]))

return false;


return isPal(myWord, -- size);
}

//Display the sentence
void Sentence :: Print()
{
cout << s [-- length];
if (length == 0)
{
cout << "" << endl;
return;

}
Print ();
}

//Main function

int main ()
{
//Holds user sentence
string userW;

//Ask user to enter a sentence
cout << "Enter a sentence: \n";
getline(cin, userW);
//Removes special characters
//This is where the ERRORS are
userW.erase(remove_if(userW.begin(), userW.end(), [](char c)
{return !isalpha(c); }), userW.end());

//Creates userSent under Sentence
Sentence userSent(userW);

//Display the number of vowels
cout << "The number of vowels in the sentence is " << userSent.numVowel << endl;
cout << "" << endl;

//Display if the sentence is a palindrome or not
cout << "The sentence" << " is" <<
(userSent.isPal(userSent.s, userSent.s.size()) ? " Palindrome\n" : " not Palindrome\n");
cout << "" << endl;
//Display the sentence backwards
cout << "The sentence spelled backwards is: " << endl;
userSent.Print();

return 0;
}

最佳答案

lambda 只是定义类的简写方式。如果您愿意,您始终可以定义一个没有 lambda 的类似类。例如,像这样的 lambda 表达式1(其中 ab 的类型为 A B分别):

[&a, =b](char c) { return a.x() + b > c; }

...可以像这样定义为显式类:

class foo { 
A mutable &a;
B b;
public:
foo(A &a, B b) : a(a), b(b) {}

bool operator()(char c) const { return a.x() + b > c; }
};

这显然要冗长得多(这也是添加 lambda 表达式的原因),但做的事情大致相同。


<支持>1. 这不是为了有用,只是为了包含两种捕获的参数。

关于c++ - 递归函数 - 删除特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33602287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com