Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:43:46+00:00 2026-05-30T11:43:46+00:00

I am working on ROT13 for c++ practice. however this bit of code here

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:43:50+00:00Added an answer on May 30, 2026 at 11:43 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with Json, how can I NSlog only the title in this code: NSDictionary
Working on this EF tutorial , I've difficult to understand the meaning of the
Working Example: This is almost identical to code I use in another places on
My site was working fine until I followed this guide, http://www.howtoforge.com/how-to-set-up-apache2-with-mod_fcgid-and-php5-on-ubuntu-9.10 to set up
Working on rewriting the front-end of my site in Coffeescript. I understand how to
Working SQL The following code works as expected, returning two columns of data (a
Working with COM interop, I can call a managed function from within unmanaged code.
Working on THIS Page: newsite.702wedding.com/live/ Can't find where to remove the arrows at the
Working through this for fun: http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/ Example calculation of nullable and first uses a
working on an old translation sample code for windows phone 7. Recently, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.