You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求编写可修正字符串中多余字符的C++程序(附示例)

Hey there! As someone who's navigated the early hurdles of C++ string manipulation, I totally get how this can feel confusing at first. Let's work through your problem step by step, starting with your specific example and then covering some more flexible approaches.

Fixing Extra Characters in C++ Strings

Your example takes the input "Excellent time too listen to music" and needs to output "Excellent time to listen to music" — so we're targeting that extra 'o' in "too". Let's break down two common ways to handle this:

1. Targeted Replacement for Known Errors

If you're dealing with specific, predictable mistakes (like accidentally writing "too" instead of "to"), the simplest approach is to search for the incorrect substring and replace it with the correct one. C++'s std::string has built-in methods to make this straightforward.

Code Example

#include <iostream>
#include <string>

int main() {
    // Use getline(cin, input) here if you want to read user input with spaces
    std::string input = "Excellent time too listen to music";
    const std::string wrong_phrase = "too";
    const std::string correct_phrase = "to";

    size_t found_pos = input.find(wrong_phrase);
    // Keep replacing until no more instances of the wrong phrase exist
    while (found_pos != std::string::npos) {
        input.replace(found_pos, wrong_phrase.length(), correct_phrase);
        // Move past the replaced text to avoid infinite loops
        found_pos = input.find(wrong_phrase, found_pos + correct_phrase.length());
    }

    std::cout << "Fixed string: " << input << std::endl;
    return 0;
}

This code will scan the entire string, swap every "too" with "to", and give you exactly the output you want. If you need to handle multiple known errors, just add more pairs of wrong/correct phrases and repeat the replacement logic for each.

2. Generalized Duplicate Character Removal

If your goal is broader — like removing any unnecessary consecutive duplicate characters (e.g., "tooo" → "to", "aaabbb" → "ab") — we can build a function that iterates through the string and keeps only non-repeating characters.

Code Example

#include <iostream>
#include <string>

std::string remove_consecutive_duplicates(const std::string& input_str) {
    if (input_str.empty()) return "";

    std::string cleaned_str;
    cleaned_str += input_str[0]; // Start with the first character

    for (size_t i = 1; i < input_str.size(); ++i) {
        // Only add the current character if it's different from the last one in our result
        if (input_str[i] != cleaned_str.back()) {
            cleaned_str += input_str[i];
        }
    }

    return cleaned_str;
}

int main() {
    std::string input = "Excellent time too listen to music";
    std::string output = remove_consecutive_duplicates(input);
    std::cout << "Cleaned string: " << output << std::endl;
    // Note: This will turn "Excellent" into "Exelent" (removes the double 'l')
    // So only use this if you want to eliminate ALL consecutive duplicates!
    return 0;
}

A quick heads-up: this method will remove every consecutive duplicate, so it might alter intentional repeats (like the double 'l' in "Excellent"). If that's not what you want, stick with the targeted replacement approach.

Quick Tips for Beginners

  • Use std::getline(std::cin, input) instead of std::cin >> input if you need to read strings with spaces — the latter stops at the first whitespace.
  • Test your code with different inputs to make sure it behaves as expected. For example, try a string with multiple "too" instances to see if the replacement catches all of them.

Hope this helps you get started! Let me know if you need clarification on any part.

内容的提问来源于stack exchange,提问作者Gaurav Kulkarni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:09:14