1

我目前是一名学习 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;
}

列出 specialChars 的函数如下

bool specialChar(char ch) {
    switch(ch) {
    case ',':
    case ':':
    case ';':
    case '.':
    case '?':
    case '!':
        return true;
    default:
        return false;
    }
}

我确实有其他功能,但它们正在工作,只是将一个单词转换为 piglatin。

4

2 回答 2

0

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

好像你想把一个字符串分成单词,这样你就可以写这样的东西

char Seperator = ' ';

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

while(std::getline(StrStream, Token, Seperator))
{
  tokens.push_back(Token);
}

既然你在向量中有单词,你可以对它们做任何你想做的事情,比如检查一个特殊的字符

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...
    }
  }
}
于 2013-10-29T14:31:31.340 回答
0

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

想想像这样分解的函数调用,以帮助您跟踪类型:

// 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 )
    {
        ...
    }
} 

以一种让您更清楚正在发生的事情的方式编写代码通常没有什么害处,当编译和优化时,它可能都是一样的。

于 2013-10-29T14:10:37.727 回答