Thread: Cryptogram troubles

  1. #1
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19

    Exclamation Cryptogram troubles

    Hello,
    I realize that this probably isn't able to be solved with a simple answer. So if you could just point me in the right direction of what I should be learning up on that would be much appreciated. I really am just clueless as to what topic this falls under to read about..

    Well, onto my question!

    How do I get my program to interpret data from a string and then alter it to then mean something else?

    IE:
    h = i
    e = f
    l = m
    o = p

    INPUT: hello
    OUTPUT: ifmmp

    Code:
    int main()
    {
    
      string subject;
      string message;
    
        cout<<"Please enter the SUBJECT: ";
        getline (cin, subject);
    
        cout<<"\n\t\t\t\t*MESSAGE*\n";
        getline (cin, message);
    
    /* How do I interpret 'message' here and replace letters
          with a different letter in its place?
    
          IE:  If the user types "hello" and now I want the program
        to recognize "h" "e" "l" "l" "o" and replace these with
          "i" "f " "m" "m" "p" */
    
        ofstream a_msg(subject.c_str());
    /* Put "ifmmp" into .txt */ 
        a_msg<<message;
        a_msg.close();
    }
    Thank you for your time!!
    Last edited by MoonMan; 02-23-2012 at 08:37 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    what you're doing is a rotation code. search google for ROT13, and you'll find lots of info on how to do this.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Two clues:

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    Code:
    	string str = "okay";
    	str[0] -= 32;
    	cout << str << endl;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19
    Hehe thank you!! This should definitely be fun

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    If you're just adding 1 to each character, this will suffice:
    Code:
    for_each(s.begin(),s.end(),[](char& c){c++;});
    Otherwise make a lookup table, denoting which char to be replaced by what:
    Code:
    void rep(char& c)//replaces everything with 'a'
    {
        bool up_flag(c>='A'&&c<='Z');
        if(up_flag)c=tolower(c);
        static char table[]=
        {
            'a','a','a','a','a',
            'a','a','a','a','a',
            'a','a','a','a','a',
            'a','a','a','a','a',
            'a','a','a','a','a',
            'a'        
        };
        c = table[c-'a'];
        if(up_flag)c=toupper(c);
    }
     //call like this:
    for_each(s.begin(),s.end(),rep);

  6. #6
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19
    Thank you! Unfortunately I have no clue how to use those examples..Hehe, so I am reading up on "Pointers" as we speak(I think that is the right direction?)...I can not figure out how to declare what is being used in the character table.

    I appreciate your reply very much but I think it is a bit past what I have learned so far

    I would not be opposed to the first way, but would later like to migrate to the table.

    Do I use this like so:
    Code:
    for_each(s.begin(),s.end(),[](char& c){c++;});
    Code:
    for_each(s.begin(),s.end(),[](char& d){d++;});
    Code:
    for_each(s.begin(),s.end(),[](char& e){e++;});
    etc. so on and so forth?





    sorry

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Oh...no.
    Code:
        for_each(s.begin(),s.end(),[](char& c){c++;});
    Suppose you have a string variable s.
    The above code replaces character element in s with the next character. EG: "ayc" gets turned into "bzd" .
    Think of it like a shorthand for this:
    Code:
    for(int i=0;i<s.length();i++)
    {
        s[i]=s[i]+1;
    }
    The table on the other hand, replaces characters with those on the table, according to alphabet position. So, the element in the second position replaces 'b' and the last one replaces 'z'.
    To use them you need to #include <algorithm> and use a compiler with good C++11 support.

  8. #8
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Posts
    19
    YES! THANK YOU

    Just had to replace the 's' with "message" and up and running with the table!

    I feel silly now

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    To use them you need to #include <algorithm> and use a compiler with good C++11 support.
    Well, if you have a compiler with really good C++11 support, then you might as well write:
    Code:
    for (char& c : message)
    {
        ++c;
    }
    If you want to use a generic algorithm instead of an explicit loop, then I think std::transform would be better: it would be slightly more verbose, but it is more descriptive than std::for_each.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. troubles in using GDB
    By leiming in forum C Programming
    Replies: 7
    Last Post: 04-18-2008, 01:54 AM
  2. GTK troubles...again...
    By cornholio in forum Linux Programming
    Replies: 4
    Last Post: 01-16-2006, 01:37 AM
  3. CD rom troubles...
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2003, 10:56 PM
  4. Cryptogram
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-16-2002, 08:06 AM
  5. cryptogram program
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-15-2001, 12:16 PM