DEV Community

Discussion on: Daily Challenge #47 - Alphabets

Collapse
 
tiash990 profile image
Tiash • Edited

In C++

#include <string>
#include <iostream>

void alphabet_position(std::string s) {
    for (int i = 0; i < s.size(); i++)
    {
        int pos = (int)((char)s[i] - 'a');
        pos = pos < 0 ? pos + ('a' - 'A') : pos;
        if (pos >= 0 && pos <= 'z' - 'a') {
            std::cout << pos+1 << " ";
        }
    }
}

int main (int argc, char *argv[])
{
    alphabet_position("<The quick brown fox jumps over the lazy dog!>");
    //print 20 8 5 17 21 9 3 11 2 18 15 23 14 6 15 24 10 21 13 16 19 15 22 5 18 20 8 5 12 1 26 25 4 15 7
    return 0;
}

Edited: there was a bug :D