Thread: Simple Encryption using an array

  1. #1
    Unregistered
    Guest

    Simple Encryption using an array

    I am very new to programming so can someone please help with this problem.
    I have managed to read a sentence into an array and changed all the characters to upper case now I have to move each character 3 places in the alphabet, so some kind of wrap around will be necessary. If you programme for a living don't worry about me stealing your job!!
    Here is my code so far:

    //Create an array.
    //invite the user to key in a sentence.
    //put the sentence into the array.
    //Then using seperate functions....
    //convert all the characters to upper case.
    // encrypt the message by moving each character 3 places in the //alphabet.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>

    void main( int argc, char *argv[], char *envp[] )

    {
    char sentence[100];

    printf ("Please enter a sentence and press enter:\n\n");

    gets(sentence);

    printf ("\nThe sentence you keyed in was:\n\n%s\n\n",sentence);

    char *p;

    for( p = sentence; p < sentence + strlen( sentence ); p++ )
    {
    if( islower( *p ) )
    _putch( _toupper( *p ) );

    else
    _putch( *p );
    }

    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Question

    I don't know if this works but...

    #include <stdio.h>
    #include <string.h>

    int main(void) {

    char algo[100];
    int i = 0;

    strcpy(algo, "Anda a la casa de tu tia");

    while (algo[i] != '\0') {
    algo[i] = algo[i] + 1;
    i++;
    }
    printf("%s", algo);
    }

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, that would work...if you are sure the string will be <100 chars and you want a simple encryption. Encryption is quite complex, but I'm sure you know that. You are probably better off writing a macro or function for the complexity of it.

    --Garfield
    1978 Silver Anniversary Corvette

  4. #4
    Unregistered
    Guest
    Thanks Garfield

    I am aware that I require a function and have tried to write one, however it will only work up to the letter 'w', here is the updated code: by the way the previous code didn't work as strcpy wasn't identified.....

    void main( int argc, char *argv[], char *envp[] )

    {
    char sentence[100];

    printf ("Please enter a sentence and press enter:\n\n");

    gets(sentence);

    printf ("\nThe sentence you keyed in was:\n\n%s\n",sentence);
    printf ("\n\nThe sentence in Uppercase is:\n\n");

    char *p;

    for( p = sentence; p < sentence + strlen( sentence ); p++ )
    {
    if( islower( *p ) )
    _putch( _toupper( *p ) );

    else
    _putch( *p );
    }

    printf ("\n\nThe sentence Encrypted is:\n\n");


    for( p = sentence; p < sentence + strlen( sentence ); p++ )
    {
    if( islower( *p ) )

    if(isalpha( *p ) )
    _putch( _toupper( *p +3) );

    else
    _putch( _toupper( *p ) );

    else

    if(isalpha( *p ) )
    _putch( *p +3);

    else
    _putch( *p );

    }

    printf ("\n\n");
    }

  5. #5
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    what you are doing is called the caesar cipher

    this is basically modulo arithmetic so you need to do


    (ch + 3)%'z'
    so that
    'x'+3 would wrapa round to 'a' and so on

    jv
    jv

  6. #6
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    rather, for case, you mean this...

    Code:
    ch = 'a' + (ch + offset) % 26;  // lower case
    ch = 'A' + (ch + offset) % 26;  // upper case
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. c++ a very simple program
    By amjad in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:59 PM
  3. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM