Pradeep L
Pradeep L

Reputation: 37

I'm getting a weird type of error while running

Write a program that will take a string S as input and will remove vowels a, e, i, o, u (in lower or upper case) from the string. If two or more than two vowels occur together, then the program shall ignore all of those vowels.

#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a < c; a++)
#define vi vector<int>
using namespace std;
int main()
{
    bool yo;
    yo = false;
    int he;
    string s = "Heel";
    int i = 0;
    // cin >> s;
    while (i < s.length())
    {

        if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')
        {

            if (s[i + 1] == 'a' || s[i + 1] == 'e' || s[i + 1] == 'i' || s[i + 1] == 'o' || s[i + 1] == 'u' || s[i + 1] == 'A' || s[i + 1] == 'E' || s[i + 1] == 'I' || s[i + 1] == 'O' || s[i + 1] == 'U')
            {
                while (s[he + 1] != 'a' || s[he + 1] != 'e' || s[he + 1] != 'i' || s[he + 1] != 'o' || s[he + 1] != 'u' || s[he + 1] != 'A' || s[he + 1] != 'E' || s[he + 1] != 'I' || s[he + 1] != 'O' || s[he + 1] != 'U' || he < s.length())
                {
                    he++;
                }
            }
            else
            {
                s.erase(remove(s.begin(), s.end(), s[i]), s.end());
            }
            i = i + he;
            he = 0;
        }
        else
        {
            i++;
        }
    }
    cout << s;
    return 0;
}

Upvotes: 0

Views: 321

Answers (2)

King
King

Reputation: 1

s = input().lower()
vowels = {'a', 'e', 'i', 'o', 'u'}  # Use a set for faster membership check
lst = []

if len(s) == 0:
    print("")

for i in range(len(s)):
    if s.count(s[i]) == 1 and s[i] in vowels:
        continue
    else:
        lst.append(s[i])

print("".join(lst))

    <pre>
    s = input().lower()
    vowels = {'a', 'e', 'i', 'o', 'u'} 
    lst = []

    if len(s) == 0:
        print("")

    for i in range(len(s)):
        if s.count(s[i]) == 1 and s[i] in vowels:
            continue
        else:
            lst.append(s[i])

    print("".join(lst))

    </pre>

Upvotes: 0

Jorge Omar Medra
Jorge Omar Medra

Reputation: 988

You are getting an access memory violation because s[he + 1] reaches another memory sector that hasn't been reserved to s.

You are creating a kind of infinity loop that runs while any vowel appears and the most important validation is at the end of conditions when it MUST be at the being to validate that there is no chance to touch an invalid sector of memory.

Your code:

while (s[he + 1] != 'a' || s[he + 1] != 'e' || s[he + 1] != 'i' || s[he + 1] != 'o' || s[he + 1] != 'u' || s[he + 1] != 'A' || s[he + 1] != 'E' || s[he + 1] != 'I' || s[he + 1] != 'O' || s[he + 1] != 'U' || he < s.length())

My suggestion:

he = 0;
bound = s.length() - 1;
while (he < bound && (s[he + 1] != 'a' || ... || s[he + 1] != 'U'))
{
  //..
  he++;
}

If he < s.length() the remainder condition wont be evaluated.

Upvotes: 2

Related Questions