C++;查找特殊字符并移动到字符串末尾 我现在是一个拿C++的学生。我的问题是我的嵌套if语句没有找到位于单词末尾的特殊字符。据我所知,它根本不运行该函数。如果有人知道怎么回事,那就太好了 #include <iostream> #include <string> using namespace std; bool isVowel(char ch); string rotate(string pStr); string pigLatinString(string pStr); bool specialChar(char ch); int main() { string str, str2, pigsentence, finalsentence, orgstr, end; int counter, length, lengtho; counter = 1; cout << "Enter a string: "; getline (cin, str); cout << endl; orgstr = str; //Add in option to move special chars string::size_type space; do { space = str.find(' ', 0); //Finds the space(s) if(space != string::npos){ str2 = str.substr(0, space); //Finds the word if(specialChar(str[true])) { //Finds special char end = str.substr(space - 1); //Stores special char as end cout << end << endl; //Testing end str.erase(space - 1); //Erases special car } str.erase(0, space + 1); //Erases the word plus the space pigsentence = pigLatinString(str2); //converst the word finalsentence = finalsentence + " " + pigsentence + end; //Adds converted word to final string }else { length = str.length(); str2 = str.substr(0, length); //Finds the word if(specialChar(str[true])) { //Finds special char end = str.substr(space - 1); //Stores special char as end cout << end << endl; //Testing end str.erase(space - 1); //Erases special car } str.erase(0, length); //Erases the word pigsentence = pigLatinString(str2); //converst the word finalsentence = finalsentence + " " + pigsentence + end; //Adds converted word to final string counter = 0; } }while(counter != 0); //Loops until counter == 0 cout << "The pig Laten form of " << orgstr << " is: " << finalsentence << endl; return 0; }

C++;查找特殊字符并移动到字符串末尾 我现在是一个拿C++的学生。我的问题是我的嵌套if语句没有找到位于单词末尾的特殊字符。据我所知,它根本不运行该函数。如果有人知道怎么回事,那就太好了 #include <iostream> #include <string> using namespace std; bool isVowel(char ch); string rotate(string pStr); string pigLatinString(string pStr); bool specialChar(char ch); int main() { string str, str2, pigsentence, finalsentence, orgstr, end; int counter, length, lengtho; counter = 1; cout << "Enter a string: "; getline (cin, str); cout << endl; orgstr = str; //Add in option to move special chars string::size_type space; do { space = str.find(' ', 0); //Finds the space(s) if(space != string::npos){ str2 = str.substr(0, space); //Finds the word if(specialChar(str[true])) { //Finds special char end = str.substr(space - 1); //Stores special char as end cout << end << endl; //Testing end str.erase(space - 1); //Erases special car } str.erase(0, space + 1); //Erases the word plus the space pigsentence = pigLatinString(str2); //converst the word finalsentence = finalsentence + " " + pigsentence + end; //Adds converted word to final string }else { length = str.length(); str2 = str.substr(0, length); //Finds the word if(specialChar(str[true])) { //Finds special char end = str.substr(space - 1); //Stores special char as end cout << end << endl; //Testing end str.erase(space - 1); //Erases special car } str.erase(0, length); //Erases the word pigsentence = pigLatinString(str2); //converst the word finalsentence = finalsentence + " " + pigsentence + end; //Adds converted word to final string counter = 0; } }while(counter != 0); //Loops until counter == 0 cout << "The pig Laten form of " << orgstr << " is: " << finalsentence << endl; return 0; },c++,string,boolean-expression,boolean-operations,C++,String,Boolean Expression,Boolean Operations,我有其他功能,但它们正在工作,只是将一个单词转换成拉丁语 将参数传递给specialChar()函数时,使用true作为数组索引!那肯定不是你想做的。解决这个问题,您可能会看到一些改进 将函数调用分解如下,以帮助您跟踪类型: // takes a char, returns a bool, so.... bool specialChar( char in ) { ... } for( int i = 0; i < str.size(); i++ ) { char aChar =

我有其他功能,但它们正在工作,只是将一个单词转换成拉丁语

将参数传递给
specialChar()
函数时,使用
true
作为数组索引!那肯定不是你想做的。解决这个问题,您可能会看到一些改进

将函数调用分解如下,以帮助您跟踪类型:

// takes a char, returns a bool, so....
bool specialChar( char in )
{ ... }

for( int i = 0; i < str.size(); i++ )
{
    char aChar = str[i];

    // ...pass in a char, and receive a bool!
    bool isSpecial = specialChar(aChar);
    if( isSpecial )
    {
        ...
    }
} 
//获取一个字符,返回一个bool,所以。。。。
布尔特殊字符(字符输入)
{ ... }
对于(int i=0;i

一般来说,编写代码的方式可以让您更清楚地了解正在发生的事情,这样做并没有什么坏处。编译和优化后,代码可能都是一样的。

您的isSpecialChar将字符作为参数,因此str[index]可以传递,但您编写的str[true]是不正确的。如果要检查字符串中是否有特殊字符,需要遍历整个字符串并检查每个字符

似乎你想把一个字符串分成几个字,这样你就可以写这样的东西了

char Seperator = ' ';

std::istringstream StrStream(str);
std::string Token;
std::vector<std::string> tokens;

while(std::getline(StrStream, Token, Seperator))
{
  tokens.push_back(Token);
}
字符分隔符=“”;
std::istringstream StrStream(str);
字符串标记;
std::向量标记;
while(std::getline(stream、Token、separator))
{
代币。推回(代币);
}
现在你有了向量中的单词,你可以做你想做的任何事情 使用它们就像检查特殊字符

for (int i = 0; i < tokens.size(); ++i)
{
  std::string& s = tokens[i];
  for (int j = 0; j < s.length(); ++j)
  {
    if ( specialChar( s[j] )
    {
      ...do whatever...
    }
  }
}
for(int i=0;i
在你的问题旁边,你是在试图创建一个参数吗?你能解释一下
if(specialChar(str[true])的意思吗
特别是参数
str[true]
?@it wasntpee我们还没有学到任何解析方面的知识,所以我要说不。@claptrap我想要做的是在变量str上运行函数specialChar,看看它是否为真。我尝试了其他格式,但我相信我没有在真的上创建数组。@aDroidman然后你需要循环这个词来检查字符例如,
for(inti=0;i
这很有意义……当从字符串转换到字符时,如何返回true/false?您使用希望检查的字符的位置作为字符串索引。
specialChar()
函数接受该字符作为参数,并将返回bool(true或false),因为它的返回类型是bool,而不是因为参数。
for (int i = 0; i < tokens.size(); ++i)
{
  std::string& s = tokens[i];
  for (int j = 0; j < s.length(); ++j)
  {
    if ( specialChar( s[j] )
    {
      ...do whatever...
    }
  }
}