C++找到特殊的字符并移动到字符串的末尾

C++ find special char and move to the end of a string

本文关键字:移动 字符串 字符 C++      更新时间:2023-10-16

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

列出特殊字符的函数如下

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

我确实还有其他功能,但它们正在工作,只需将一个单词转换为猪拉丁。

你的isSpecialChar接受一个字符作为参数,所以str[index]将是你可以传递的东西,但你写str[true]是不正确的。如果你想检查你的字符串中是否有一个特殊的Char,你需要遍历整个字符串并检查每个字符。

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

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...
    }
  }
}

在将参数传递给 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 )
    {
        ...
    }
} 
通常,以

使您更清楚地了解正在发生的事情的方式编写代码是没有害处的,当编译和优化时,它们可能都是一样的。