Thread: manipulating strings

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    manipulating strings

    Can someone give me a hint on how to start. Im working within visual studio c++ 2003

    obtain a word/sentence (up to 80 characters) with spaces from the user

    2 fuctions:

    1. encrypt the word/sentence

    2. decrypt the word/sentence

    display both the encrypt and decrypt word/sentence

    loop if they want to enter another

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    1. encrypt the word/sentence

    2. decrypt the word/sentence
    What kind of encryption ?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1. encrypt the word/sentence
    >2. decrypt the word/sentence
    It doesn't even have to be two functions.
    Code:
    void cryptykins ( string& s, unsigned key )
    {
      for ( int i = 0; i < s.length(); i++ )
        s[i++] ^= key;
    }
    More or less.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    nope need 2 functions

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >nope need 2 functions
    Try it and see. XOR encryption can be implemented using only one function for both purposes. But if your teacher insists that you have two functions you can do this:
    Code:
    void encrypt ( string& s, unsigned key )
    {
      for ( int i = 0; i < s.length(); i++ )
        s[i++] ^= key;
    }
    
    void decrypt ( string& s, unsigned key )
    {
      for ( int i = 0; i < s.length(); i++ )
        s[i++] ^= key;
    }
    My best code is written with the delete key.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Surely this would add to the teacher confusion
    Code:
    void decrypt ( string& s, unsigned key )
    {
      encrypt( s, key );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating C strings
    By black_spot1984 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 10:23 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. manipulating strings
    By rain_e in forum C Programming
    Replies: 14
    Last Post: 03-05-2002, 03:15 PM