简体   繁体   English

如何将一个字符串复制到另一个字符串?

[英]How do you copy a string to a different string?

I am writing a function that removes vowels from a string (users name), and will then pass that function into another function that prints out the name without vowels.我正在编写一个 function,它从字符串(用户名)中删除元音,然后将 function 传递给另一个 function,打印出没有元音的名称。 In order to copy the input[i] to the outputNoVowels , would this be the correct way of doing it?为了将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;
}

How come, when the program runs, it only prints out one letter in the string?为什么程序运行时只打印出字符串中的一个字母呢?

Provided Algorithm:提供的算法:

图片

This is what is printing in the console window:这是在控制台 window 中打印的内容:

图片


UPDATE : how would I do the same for char[] strings?更新:我如何对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);
        }
    }
}

Provided Algorithm:提供的算法:

图片

You are getting 1 letter in the output because you are assigning each letter to outputNoVowels via its = assignment operator, wiping out its current content each time, so there is at most only 1 letter in outputNoVowels upon return .您在 output 中得到 1 个字母,因为您通过其=赋值运算符每个字母分配给outputNoVowels ,每次都清除其当前内容,因此outputNoVowels中最多只有 1 个字母 upon return

You need to instead append each letter to outputNoVowels , via its += operator (or its push_back() or append() method), thus preserving any existing content.您需要通过其+=运算符(或其push_back()append()方法)将每个字母outputNoVowels改为outputNoVowels ,从而保留任何现有内容。

Also, your loop is ignoring the last character in the input string.此外,您的循环忽略了input字符串中的最后一个字符。 There is a typo in the instructions.说明中有错字。

Try this instead:试试这个:

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

That being said, you can eliminate the switch altogether by using the standard std::copy_if() algorithm instead (with std::back_inserter as the destination), eg:也就是说,您可以通过使用标准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;
}

Or, by using the std::remove_if() algorithm, eg:或者,通过使用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;
}

Or, by using the std::erase_if() algorithm (C++20 and later), eg:或者,通过使用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;
}

UPDATE : in your char[] version, you have the opposite problem.更新:在您的char[]版本中,您遇到了相反的问题。 You are appending too many letters to the output string.您向 output 字符串附加了太多字母。 For each letter you want to keep, you are assigning the entire input string to the output, and then appending the entire input string to the end, rather than just the 1 letter.对于要保留的每个字母,您将整个输入字符串分配给 output,然后将整个输入字符串附加到末尾,而不仅仅是 1 个字母。

You can use techniques similar to above to solve the char[] version, eg:您可以使用与上述类似的技术来解决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';
}

Or, using the std::copy_if() algorithm:或者,使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM