jeudi 29 mars 2018

Erasing an element of a string using its index and str.erase?

How I use the erase function to erase part of a string "str" in the form of str.erase(str[i]) ?

When I run, it outputs the string minus the first element only, and then some weird characters.

Code :

#include<bits/stdc++.h>
using namespace std; 
int main()
{
    string s;
    cout << "Please input the desired string : \n";
    cin >> s;
    int sz = sizeof s;
    int x = sz / 2;
    if ((sizeof s)%2 != 0)
    {
        s.erase(s[x]);
    }
    else
    {
        s.erase(s[x-1], s[x]);
    }
    for (int j=1; j <= sz; j++)
    {
        cout << s[j];
    }    
}

Aucun commentaire:

Enregistrer un commentaire