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 declaredint he;but never set an initial value. When you start usinghein 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 accesss[he+1]even whenheis already at the end of the string, causing an out-of-bounds error. - Incorrect use of
remove(): Theremovefunction erases all occurrences of the character you pass it, not just the current one. So if you have a string like "Apple", callingremoveon 'p' would delete both 'p's, which isn't what you want. - Messy index management: Your
i = i + heline doesn't make sense (sincehestarts at 0), and when you erase characters from the string, the length changes—directly incrementingican lead to skipping characters or going out of bounds. - Hardcoded test string: You commented out
cin >> sand sets = "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
isVowelhelper 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
resultstring. 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
相关产品推荐
相关产品推荐

