[英]How do you copy a string to a different string?
我正在編寫一個 function,它從字符串(用戶名)中刪除元音,然后將 function 傳遞給另一個 function,打印出沒有元音的名稱。 為了將input[i]
復制到outputNoVowels
,這是正確的做法嗎?
string removeVowels(string input) {
string outputNoVowels;
for (int i = 0; i < input.length() - 1; i++) {
switch (input[i]) {
case 'a':
break;
case 'e':
break;
case 'i':
break;
case 'o':
break;
case 'u':
break;
case 'A':
break;
case 'E':
break;
case 'I':
break;
case 'O':
break;
case 'U':
break;
default:
outputNoVowels = input[i];
}
}
return outputNoVowels;
}
為什么程序運行時只打印出字符串中的一個字母呢?
提供的算法:
這是在控制台 window 中打印的內容:
更新:我如何對char[]
字符串做同樣的事情?
void removeVowels(const char input[], char output[]) {
for (int i = 0; i < strlen(input) - 1; i++) {
switch (input[i]) {
case 'a':
break;
case 'e':
break;
case 'i':
break;
case 'o':
break;
case 'u':
break;
case 'A':
break;
case 'E':
break;
case 'I':
break;
case 'O':
break;
case 'U':
break;
default:
strcpy(output, input);
strcat(output, input);
}
}
}
提供的算法:
您在 output 中得到 1 個字母,因為您通過其=
賦值運算符將每個字母分配給outputNoVowels
,每次都清除其當前內容,因此outputNoVowels
中最多只有 1 個字母 upon return
。
您需要通過其+=
運算符(或其push_back()
或append()
方法)將每個字母outputNoVowels
改為outputNoVowels ,從而保留任何現有內容。
此外,您的循環忽略了input
字符串中的最后一個字符。 說明中有錯字。
試試這個:
string removeVowels(const string &input) {
string outputNoVowels;
const size_t len = input.length();
for (size_t i = 0; i < len; ++i) {
switch (input[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
break;
default:
outputNoVowels += input[i];
// or: outputNoVowels.push_back(input[i]);
// or: outputNoVowels.append(1, input[i]);
break;
}
}
return outputNoVowels;
}
也就是說,您可以通過使用標准std::copy_if()
算法(以std::back_inserter
作為目標)來完全消除switch
,例如:
string removeVowels(const string &input) {
static const string vowels = "aeiouAEIOU";
string outputNoVowels;
copy_if(input.begin(), input.end(),
back_inserter(outputNoVowels),
[&](char ch){ return vowels.find(ch) == string::npos; }
);
return outputNoVowels;
}
或者,通過使用std::remove_if()
算法,例如:
string removeVowels(string input) {
static const string vowels = "aeiouAEIOU";
input.erase(
remove_if(input.begin(), input.end(),
[&](char ch){ return vowels.find(ch) != string::npos; }
),
input.end()
);
return input;
}
或者,通過使用std::erase_if()
算法(C++20 及更高版本),例如:
string removeVowels(string input) {
static const string vowels = "aeiouAEIOU";
erase_if(input,
[&](char ch){ return vowels.find(ch) != string::npos; }
);
return input;
}
更新:在您的char[]
版本中,您遇到了相反的問題。 您向 output 字符串附加了太多字母。 對於要保留的每個字母,您將整個輸入字符串分配給 output,然后將整個輸入字符串附加到末尾,而不僅僅是 1 個字母。
您可以使用與上述類似的技術來解決char[]
版本,例如:
void removeVowels(const char input[], char output[]) {
int idx = 0;
const int len = strlen(input);
for (int i = 0; i < len; ++i) {
switch (input[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
break;
default:
output[idx++] = input[i];
break;
}
}
output[idx] = '\0';
}
或者,使用std::copy_if()
算法:
void removeVowels(const char input[], char output[]) {
static const char[] vowels = "aeiouAEIOU";
static const char* vowels_end = vowels + strlen(vowels);
char* output_end = copy_if(input, input + strlen(input),
output,
[&](char ch){ return find(vowels, vowels_end, ch) == vowels_end; }
);
*output_end = '\0';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.