Search code examples
c++stringstrchr

strchr not working with char[]


I am working on ROT13 for c++ practice. however this bit of code here returns an error and fails to compile, i do not understand why! I am posting a snippet of code in the following lines

string encode(string &x)
{
    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";

    for (size_t l=0;l<x.size();++l){
        cout<<x[l];
        cout<< strchr(alphabet,x[l]);    
    }
    return x;
}

Q2. Also help me return the index of the matching letter from alphabet[] (e.g.,5 for 'f') to which i can add 13 and append that to x and so on ..

Q3. Besides practice, which course in CS would help me develop more efficient algorithms? Is it theory of computation, discrete mathematics, or algorithms ?


Solution

  • In order, starting with question 1:

    The following compiles fine for me:

    #include <iostream>
    #include <cstring>
    
    std::string encode(std::string &x)
    {
        char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
        char *ptr;
        for (size_t l=0;l<x.size();++l){
            std::cout<<x[l];
            std::cout<< std::strchr(alphabet,x[l]);
        }
        return x;
    }
    
    int main (int argc, char* argv []) {
        return 0;
    }
    

    Make sure:

    • you include the headers given, for cout and strchr.
    • use std:: prefixes unless you're using the std namespace.
    • fix that ptr problem.

    Question 2:

    If you're looking for a handy ROT-13 method, consider using two C strings, one for the source and one for the translation:

    char from[] = "abcdefghijklmnopqrstuvwxyz";
    char to  [] = "nopqrstuvwxyzabcdefghijklm";
    

    Then you can use strchr to look it up in the first one and use that pointer to find the equivalent in the second.

    char src = 'j';
    char *p = strchr (from, src);
    if (p == NULL)
        std::cout << src;
    else
        std::cout << to[p - from];
    

    That would output the character as-is if it wasn't found or look up the translation if it was found. You may also want to put the capital letters in there as well.

    Question 3:

    If you want to learn about efficient algorithms, I'd go for, surprisingly enough, an algorithms course :-)

    Theory of computation sounds a little dry, though it may well cover the theoretical basis behind algorithms. Discrete mathematics has applicability to algorithms but, again, it's probably very theoretical. That's all based on what the words mean, of course, the actual subject areas covered may be totally different, so you should probably take it up with the people offering the courses.

    Extra bit:

    If you're looking for something to compare your own work to, here's one I put together based on my suggestions above:

    #include <iostream>
    #include <cstring>
    
    std::string rot13 (std::string x)
    {
        char from[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char to  [] = "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM";
        std::string retstr = "";
        for (size_t i = 0; i < x.size(); ++i) {
            char *p = std::strchr (from, x[i]);
            if (p == 0)
                retstr += x[i];
            else
                retstr += to[p - from];
        }
        return retstr;
    }
    
    int main (int argc, char* argv []) {
        std::string one = "This string contains 47 and 53.";
        std::string two = rot13 (one);
        std::string three = rot13 (two);
        std::cout << one << '\n';
        std::cout << two << '\n';
        std::cout << three << '\n';
        return 0;
    }
    

    The building of the return string could have probably been done more efficiently (such as a new'ed character array which becomes a string only at the end) but it illustrates the "lookup" part of the method well.

    The output is:

    This string contains 47 and 53.
    Guvf fgevat pbagnvaf 47 naq 53.
    This string contains 47 and 53.
    

    which you can verify here, if necessary.