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

C++字符串去元音功能实现代码运行报错排查咨询

Fixing Your C++ Vowel Removal Code

First off, let's walk through all the bugs in your code that are causing those weird errors:

  • Uninitialized variable he: You declared int he; but never set an initial value. When you start using he in your while loop, it's holding a random garbage value, which leads to undefined behavior (like crashing or incorrect jumps).
  • Broken continuous vowel loop logic: The condition in your inner while loop is backwards. You used || instead of && for checking non-vowels, and you put the bounds check (he < s.length()) at the end—this means you might access s[he+1] even when he is already at the end of the string, causing an out-of-bounds error.
  • Incorrect use of remove(): The remove function erases all occurrences of the character you pass it, not just the current one. So if you have a string like "Apple", calling remove on 'p' would delete both 'p's, which isn't what you want.
  • Messy index management: Your i = i + he line doesn't make sense (since he starts at 0), and when you erase characters from the string, the length changes—directly incrementing i can lead to skipping characters or going out of bounds.
  • Hardcoded test string: You commented out cin >> s and set s = "Heel"—this is fine for testing, but your final code should accept user input.

Simplified Correct Solution

The easiest and most reliable way to solve this problem is to build a new string that only includes non-vowel characters. This avoids all the index headaches of modifying the original string in place:

#include <iostream>
#include <string>
#include <cctype> // For tolower()
using namespace std;

// Helper function to check if a character is a vowel (case-insensitive)
bool isVowel(char c) {
    char lowerC = tolower(c);
    return lowerC == 'a' || lowerC == 'e' || lowerC == 'i' || lowerC == 'o' || lowerC == 'u';
}

int main() {
    string s;
    cout << "Enter your string: ";
    cin >> s;
    
    string result;
    for (char currentChar : s) {
        // Only add the character to the result if it's NOT a vowel
        if (!isVowel(currentChar)) {
            result += currentChar;
        }
    }
    
    cout << "Result after removing vowels: " << result << endl;
    return 0;
}

How This Works

  • The isVowel helper function converts characters to lowercase first, so we don't have to check both uppercase and lowercase vowels separately—this cleans up the code a lot.
  • We iterate over every character in the input string: if it's not a vowel, we add it to our result string. This automatically skips all vowels, whether they're single or consecutive.
  • No messy index manipulation or string erasure means we avoid out-of-bounds errors and undefined behavior.

In-Place Modification Alternative (If You Prefer)

If you want to modify the original string instead of creating a new one, you can use a two-pointer approach to overwrite vowels with non-vowel characters, then truncate the string:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

bool isVowel(char c) {
    char lowerC = tolower(c);
    return lowerC == 'a' || lowerC == 'e' || lowerC == 'i' || lowerC == 'o' || lowerC == 'u';
}

int main() {
    string s;
    cout << "Enter your string: ";
    cin >> s;
    
    int writeIndex = 0;
    for (int readIndex = 0; readIndex < s.size(); ++readIndex) {
        if (!isVowel(s[readIndex])) {
            s[writeIndex++] = s[readIndex];
        }
    }
    // Truncate the string to the length of non-vowel characters
    s.resize(writeIndex);
    
    cout << "Result after removing vowels: " << s << endl;
    return 0;
}

This approach uses less memory since it doesn't create a separate string, but the first solution is more readable for most cases.

内容的提问来源于stack exchange,提问作者Pradeep L

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 16:57:49