1

If I have a string with 50 characters like

a = "ZfJt0\qVwy<aGWdkC5[HI[A2<ux1ajSI4I1OotE`uZo0ZErf0d"

How can I use c++ to delete the symbols from the string?

Only the characters a-z and A-Z can stay in the string, and all the others need to be removed. Using the example above the result has to be

"ZfJtqVwyaGWdkCHIAuxajSIIOotEuZoZErfd"

I have tried in the following way, but it doesn't work

for (int i = 0; i < a.length(); i++) {
    if (!((int)a[i] > 64 && (int)a[i] < 91) || !((int)a[i] > 96 && (int)a[i] < 123)) {
        for (int j = i; j < a.length(); j++) {
            if (j == 49) {
                a[j] = ' ';
            } else {
                a[j] = a[j + 1];
            }
        }
    }
}
Arty
  • 14,883
  • 6
  • 36
  • 69
  • What kind of symbols do you want to delete? How you determine which you need to delete? For example to delete just digits (numbers)? – Arty Mar 10 '21 at 08:44
  • Hello, thank you for helping. only the characters a-z and A-Z can stay in the string, and all the others need to be removed. using the same example, the result has to be "ZfJtqVwyaGWdkCHIAuxajSIIOotEuZoZErfd" – Little Little Sam Mar 10 '21 at 08:47
  • Please read the [tour]: _"Don't ask about... Questions you haven't tried to find an answer for (show your work!)"_ Your current question is unrelated to C++. Loop over each element of the string and check if it's a letter. This algorithm works for most (if not all) programming languages – Thomas Sablik Mar 10 '21 at 08:48
  • @LittleLittleSam Look [at this code](https://tio.run/##ZU5dT4MwFH3nV1zrC4gszOjLgCWLWRRfTGaiCcxghQupY2WWEnHG345tFx2J96E5956Pnny38/Ka8moYThnP665ACFspGK/m1vEisMJ@fGCNEiHdzi2LcQlbyrjtwJcFalpZzGaHDJDYS4iAJOWd9Nfr98ePz5DePBWb66v0Nk4XF2HXT@nbQ3wZT@8buXzpksZPlqL0CxIc08z/INBeETul3n7hJc@uQ5yRpDWaTDYb5BmTKKhsBGhg6xKTV6x0x3NTaYK80FigehQO/hUX2GZaeWBKFWUHJg1OIuMA19WrY3g9vw5wIzjT1Cg0bzoJYQhkhW1XyxkQvf05FDYylVsH1vcw/AA), I solved your task using standard `` module. – Arty Mar 10 '21 at 08:56
  • @LittleLittleSam I implemented [one more solution](https://tio.run/##VY7BSgMxEIbv@xTDetlCLVvRSxoFkaLrRVCwsC3UNMluY9NkTSbYIj77NmkP1bnMzD//fDO86y65Zqbt@wtluA5CAvXolGnvsrOibNQk2/7VmG6tU7iOYqYMwpYpUwzgJ4MYHgUhJw446YeAcodwC3ndPGO5WHy9f@8pe5yJzcPN/Kma31/RsBuzz7fquhq/WJx@hNqW9dQ1pcgnZyS33X6pmiLhRivZppMn@EgakeqjbcX4ZqmMlw6lK@IDcUCI8kx3azb4xwsIlEL@Kn3QSCBPXVxI6eiIWD3Jfvv@AA) that is even more simple (without regular expressions), that uses standard `std::copy_if`, but this solution is only suitable for your case only for range `a-zA-Z`. For complex criteria of symbols use solution from comment above. – Arty Mar 10 '21 at 09:06
  • @LittleLittleSam Also did you know that in your code instead of `64`, `91`, `96`, `123` you could use just `'A' - 1`, `'Z' + 1`, `'a' - 1`, `'z' + 1` which is more readable. You can see these values and corresponding symbols inside [this ascii table](http://www.asciitable.com/). – Arty Mar 10 '21 at 09:27
  • @LittleLittleSam Although your own solution is not very efficient it is close to correct, I decided to correct mistakes in it [here is by this link](https://tio.run/##XVBda4MwFH3Pr7hzUCJF0a4rVDNhjLK5l8EGG2iFBY0uzsZOI/uiv90ljmHdfbiHe3LPuYek@72VVlQUfX/KRVp1GQPSyoaLIkAjw2vFMboLEOJCwo5ygU34RqCqlZnn/UqAwgUYUX4rne327fH9k9Drp@z16jy@CePLBek@XFo@hMvQvavl5rmLaifaNLmTGf5gldcNYH2AKx/HV0CA2hUThXzBpprn87@rungO@ARrgUljnkAAqyXMZjAyBNauqanp2nr1f81dnJnH1pM0pUrDfQXHacACV3Eq0USki8ZloiQKYA5u4k8WqN2wln8xTO0BBiNzumNZfCQOaOzDX6d1J4EQMO5Z21XSA0NPVLfhnYms8tGh738A) is your corrected working code. – Arty Mar 10 '21 at 09:41

0 Answers0