Print first repeating character of input string in C++ : Memory Optimized



#include <iostream>

#define CHAR_TO_INT(c) (c - 'a' + 1)
int main(int argc, char** argv)
{
    std::string strInput;
    std::cout << "Get any string:\t";
    std::cin >> strInput;
    int count = 0;

    for (int i = 0; i < strInput.length(); i++) {

        int val = CHAR_TO_INT(strInput[i]);
        if ((count & (1 << val)) > 0) {
            std::cout << "Input string has first non-uniques character \t" << strInput[i];
            break;
        }
        count |= (1 << val);
    }

    return 0;
}

No comments :

Post a Comment

Comment Here