Like Tree2Likes
  • 1 Post By manasij7479
  • 1 Post By laserlight

Cryptogram troubles

This is a discussion on Cryptogram troubles within the C++ Programming forums, part of the General Programming Boards category; Hello, I realize that this probably isn't able to be solved with a simple answer. So if you could just ...

  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 07:37 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,297
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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);
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    MoonMan likes this.

  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
    21,445
    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.
    manasij7479 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:37 AM
  3. CD rom troubles...
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2003, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21