-2

so in c++ 'A' and 'a' are different characters, if we have a vector that contains both upper and lowercase letters, how to write a function that transforms this vector into some vector that is case insensitive, for example, 'ABba' becomes the same as 'abba'.

so for example, I want to count the number of different characters within the string, for example, "ABba" in this case output must be 2, because A a are 1 same group, B and b same as well, this string also may contain numbers, for example. ABba1212 --> answer should be 4.

math boy
  • 25
  • 4
  • 2
    Is there a reason a for loop through the vector, and change each character doesn't satisfy what you were looking for? – Ranoiaetep Nov 22 '21 at 22:09
  • What is the higher-level problem you're trying to solve? For example is it that you have two strings and you want to check if they're equal, ignoring case? Are you using `vector` or `vector`? – John Zwinck Nov 22 '21 at 22:41
  • 2
    The "correct" way would be a `std::string` with case-insensitive `char_traits`. But *true* case "insensitivity" is a MUCH more complicated issue if you want to get it *internationally* right. So complicated in fact that even C++ `std::string` can't do it (because you need much more context than `std::string` delivers), and we will see a completely different approach with `std::text` some time in the future... -- So why don't you tell us exactly what this is for / about, so we can pick something dumbed-down *just enough* on the Unicode-to-ASCII-7 scale? ;-) – DevSolar Nov 22 '21 at 22:51
  • 2
    Read about [`std::tolower`](https://en.cppreference.com/w/cpp/string/byte/tolower). – Pete Becker Nov 22 '21 at 23:11

1 Answers1

0

The standard approach for doing case insensitive comparisons is:

Decide for either upper or lower case and convert all letters to this case. Then, do your operations.

In C++ you have a family of functions for that purpose. std::toupperand std::tolower. Please check in CPP Reference.

If you know what character set you have, there are also other possibilities. In western countries very often ASCII characters are uses. In that case, you can even do bit operations for conversions. Or, additions or subtractions.

Some examples for converting to lower case with ASCII characters

#include <iostream>
#include <cctype>

int main() {

    char c = 'A';
    c = (char)std::tolower(c);
    std::cout << "\nWith tolower:  " << c << '\n';

    c = 'A';
    if (c >= 'A' and c <= 'Z') c += ('a' - 'A');
    std::cout << "\nWith addition:  " << c << '\n';

    c = 'A';
    if (c >= 'A' and c <= 'Z') c |= 32;
    std::cout << "\nWith bit operation:  " << c << '\n';
}

Next, counting different characters. If you want to count the different characters in a string, then you need to iterate over it, and check, if you saw the character or not. There are really many different solutions for that.

I will show you a very basic one and then a C++ solution.

It is made for 8 bit char values.

#include <iostream>
#include <cctype>
#include <string>

int main() {

    std::string test{"ABba1212"};

    // There are 256 different 8 bit char values. Create array and initialize everything to false
    bool weHaveACharValueForThisASCII[256] = {};

    // Iterate over all characters in the source string
    for (char c : test)
        // And mark, if we found a certain char
        weHaveACharValueForThisASCII[std::tolower(c)] = true;

    // Now we want to count, how many different chars we found
    int sum = 0;
    for (bool b : weHaveACharValueForThisASCII) if (b) ++sum;

    // Show result
    std::cout << sum;

    return 0;
}

In C++ you would use a std::unordered_set for this. It can only contain unique values and uses fast hashing fordata access. Please see here.

#include <iostream>
#include <cctype>
#include <string>
#include <unordered_set>

int main() {

    std::string test{"ABba1212"};

    // Here we will store the unique characters
    std::unordered_set<char> unique{};

    // Iterate over all characters in the source string
    for (char c : test) unique.insert((char)std::tolower(c));

    // Show result
    std::cout << unique.size();
}
A M
  • 14,694
  • 5
  • 19
  • 44