C++找到特殊的字符并移动到字符串的末尾
C++ find special char and move to the end of a string
我目前是一名参加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 )
{
...
}
}
通常,以使您更清楚地了解正在发生的事情的方式编写代码是没有害处的,当编译和优化时,它们可能都是一样的。
相关文章:
- 通过字符串来计算长度需要更长的时间,而不是移动字符串几次?
- 尝试移动字符串中的每个位
- 为什么字符串流只读取整数中的第一个数字?如何使它只在空白处移动?
- 错误:移动文件时'QString'为非标量类型"std::__cxx11::字符串"
- 快速从文件中读取下一个字符串,而无需移动文件的位置
- 如何找到通过仅移动相邻字符创建的字符串的排列?-C++
- 通过单词列表将两个字符串移动以比较每个单词
- STD ::移动性能,同时将大字符串作为rvalue
- 如何在循环移动后从给定的字符串中找到一组不同的字符串
- 如何移动 std::ostringstream 的底层字符串对象?
- 从字符串和向量移动以不拥有任何堆内存
- 字符串将第一个单词移动到字符串的末尾
- 如何在不删除任何字符的情况下将字符从字符串中的某个位置移动到其最前面
- 需要从格式化字符串移动到二进制缓冲区
- 如何在调用擦除C++后向上移动字符串向量的内容
- 在 vs2012 中移动字符串
- 在c++数组中移动字符串值的问题
- 移动字符串中的单词,单词之间使用空格作为分隔符
- 正在移动字符串
- 使用函数在C++中移动字符串