Thread: Encipher

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Encipher

    K, hay hay boys. I'm preety noob at this so keep the responses low-key. Basically i'm trying to create a C program that after i enter in a string/sentence, it spits it back out coded ( encrypted ).

    Its a very basic encryption, just if you enter 'a' it comes out 'b'. 'c' comes out 'd'. Basically + 1 letter, if its then 'z' = 'a'. I managed to do it entering in single characters at a time using a switch function to encode it. But one letter at a time isn't exactly time-saving.
    Code:
    #include <string.h>
    #include <stdio.h>
    
    main() {
    
    
           char input[256];
           char ia;
           char ib;
           char ic;
           char id;
           char ie;
           char i_f;
           char ig;
           char ih;
           char ii;
           char ij;
           char ik;
           char il;
           char im;
           char in;
           char io;
           char ip;
           char iq;
           char ir;
           char is;
           char it;
           char iu;
           char iv;
           char iw;
           char ix;
           char iy;
           char iz;
           
           // Variables *** FINISH ***
           
           // Declarations *** START ***
           
            ia = 'b';
            ib = 'c';
            ic = 'd';
            id = 'e';
            ie = 'f';
            i_f = 'g';
            ig = 'h';
            ih = 'i';
            ii = 'j';
            ij = 'k';
            ik = 'l';
            il = 'm';
            im = 'n';
            in = 'o';
            io = 'p';
            ip = 'q';
            iq = 'r';
            ir = 's';
            is = 't';
            it = 'u';
            iu = 'v';
            iv = 'w';
            iw = 'x';
            ix = 'y';
            iy = 'z';
            iz = 'a';
    //Declarations ***FINISH***
    
    char input[256];
    fgets( input, 256, stdin);
    Now i don't know what to do from here, i don't know how to examine each string and change it. I know its something to do with strlen but just don't know how to.
    Last edited by mf-; 02-06-2006 at 02:04 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well, first you need to iterate through the string. Something like:

    Code:
    char input[256];
    char *iter;
    fgets( input, 256, stdin);
    iter = input;
    while(*iter != '\0')
    {
       if(*iter == 'a')
          *iter = 'a' + 1;
    
       iter++;
    }
    That code should get you on the right track. What I placed inside the brackets {...} is just one way of doing the character conversion. There are other (better) ways of doing it, but this is just to get you thinking about the problem. Remember that fgets() can append a '\n' to the end of the string, so you need to be able to handle that.

Popular pages Recent additions subscribe to a feed