help using strings and mapping

This is a discussion on help using strings and mapping within the C Programming forums, part of the General Programming Boards category; i need help understanding the logic and what i am supposed to do for my assignment. I have done part ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Question help using strings and mapping

    i need help understanding the logic and what i am supposed to do for my assignment. I have done part one and now part two is the following "
    The second part of the file contains the text to be encrypted. There is no limit on the number of lines, but no line will be over 80 characters long. Each alphabetic character (upper or lower case) will be replaced according to the cipher key selected based on the random number generation and the first 5 lines of the input file. Both the uppercase A and the lower case a are the first element of the alphabet and will both be replaced by the lower case letter that appeared first on the line selected for the cipher key (and now should be in the 0th element of the array). Any non-alphabetic character that appears on that line will just be maintained without any modification."
    Here is the example:  If the agreed upon Key is
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    i y k w v u t s a g p o c n l x r z h r b e d m f j
     And the original message is
    Hi, Jan
     The encrypted message is
    sa, gin


    is what I am thinking on the right track. First check to see if the character in original message is a letter using isalpha then change to lower case using tolower and then mapp the characters which is where I am stuck. here is the code i have so far (as a function to be called in main)
    Code:
    void encrypt(char secret[],char message[], char coded[]){ 
       int count, next; 
       char letter; 
       for(count = 0,count < MAXLEN,count++){        /* CHECK TO SEE IF LETTER */ 
          letter = fgets(message,MAXLEN,stdin);         /* GET LETTER FROM MESSAGE */ 
          if(isalpha(letter)){                                             /* CHANGE TO LOWER CASE */ 
             for(next = 0; message[next] != '\0';next++){ 
                 message[next] = tolower(message[next]); 
             } 
          } 
       }

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    and MAXLEN is 81

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Well, your syntax is all wrong for one, so it's a guess as to whether you understand what you've written. For instance, fgets() does not return a character.

    You didn't post all the information to answer your question about the part you are stuck on. You show that zero maps to i. But am I to assume that 'A' maps to zero?

    Todd

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    oh ok. yes A maps to 0. so fgets gets an entire string if it'n not a character. do i use getchar()? what do i need to fix

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Code:
    this is the main function
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAXLEN 81
    #define ALPHABETSIZE 26
    
    void getKey(char [],int *);
    void encrypt(char[], char[], char[]);
    
    int main(){
        char cryptArr[ALPHABETSIZE];
        char original[MAXLEN];
        char encrypted[MAXLEN] = "";
        int keyNum;
        int i = 0;
        getKey(cryptArr,&keyNum);
        printf("selected key #%d\n",keyNum);
        while (fgets(original,MAXLEN,stdin)){
            encrypt(cryptArr, original, encrypted);
            printf("%s",encrypted);
            i++;
        } 
        return 0;
    }

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    void encrypt(char secret[],char message[], char coded[])
    { 
       int count, next; 
       char letter; 
    
       for(count = 0,count < MAXLEN,count++)
       {
           letter = tolower(message[count]);
           if(isalpha(letter))
           { /*Do your encryption here; */ }  
       }
    }
    ssharish

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    oh ok..thanks....that makes sense. what is the logic or how to I go about doing the encryption. i was thinking that i have to mapp the index with the key. but i'm not sure where to start. i know that if 'a' maps to index 0, then every mapping is something minus 'a'. not to sure

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    i suppose you should do it something like this, assuming that your cipher key are in coded char array.

    Code:
    if(isalpha(letter))
    {
        message[count] = coded[letter - 'a']; 
    }
    ssharish
    Last edited by ssharish2005; 11-29-2007 at 10:55 PM.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    sorry , i'm a beginner, can you explain what that line means
    Code:
    if(isalpha(letter))
    {
        message[count] = coded['a' - letter]; 
    }
    is it saying that the 'a' minus letter position in the coded array is then stored in message array?

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    if u can think of

    coded[0] = 'i';
    coded[1] = 'y';
    coded[2] = 'k';
    .
    .
    and soo on
    So in place of 'a' u want i and in place of 'b' u want y and so on

    Code:
    coded[letter - 'a'];
    So what this does is indexes through coded array to get the right char. So if the letter had 'b'.
    letter - 'a'
    The chars will be converted to their ascii values and the gets difference. So
    98 -97 = 1 which is nothing but coded[i] ==> 'y'

    ssharish

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    OK That makes sense. thanks. now where does the third parameter come in. i take in three arrays. secret[] is the key, message[] is the original input that needs to be encrpted, and coded is the message encrypted.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    does this work
    Code:
    void encrypt(char secret[],char message[],char coded[]){    
      int count; 
      char letter;     
      for(count = 0;count < MAXLEN;count++){     
        letter = message[count];   
        if(isalpha(letter)){                        /*CHECK TO SEE IF LETTER*/      
          message[count] = tolower(message[count]); /*CHANGE TO LOWER CASE*/ 
          message[count] = secret[letter - 'a'];    /*START OF ENCRYPTION*/     
          coded[count] = message[count]; 
        }    
      }  
    }

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It doesn't look obviously wrong - but I can't say for sure.

    Of course, all references to message[count] can be replaced by "letter" [aside from where you assign letter, of course], which would:
    1. Not change the original message.
    2. Make the code a little bit easier to read.
    3. Possibly improve the performance of the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    ok. can you look at my program. my program generates a random number 1-5, reads the input text of five lines and the original message, the key number generated corresponds to one of the five lines to use to encrypt the message. then the message is ecrypted.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAXLEN 81
    #define ALPHABETSIZE 26
    
    void getKey(char [],int *);
    void encrypt(char[], char[], char[]);
    
    int main(){
        char cryptArr[ALPHABETSIZE];
        char original[MAXLEN];
        char encrypted[MAXLEN] = "";
        int keyNum;
        int i = 0;
        getKey(cryptArr,&keyNum);
        printf("selected key #%d\n",keyNum);
        while (fgets(original,MAXLEN,stdin)){
            encrypt(cryptArr, original, encrypted);
            printf("%s",encrypted);
            i++;
        } 
        return 0;
    }  
    
     void getKey(char secret[], int *keycode){
        int count, keynum, randkey;  
        int cycle = 0;
        char temparr[ALPHABETSIZE + 2];  
        srand(time(NULL)); 
        randkey = rand();  
        keynum = randkey % 5 + 1;      /* RANGE CHECK */  
        for(count = 0;count < 5;count++){  
           fgets(temparr,ALPHABETSIZE + 2,stdin);  
           if (count == keynum){ 
              while(cycle < ALPHABETSIZE){ 
              secret[cycle]=temparr[cycle]; 
              }  
           } 
           *keycode = keynum;  
        } 
    } 
    
    void encrypt(char secret[],char message[],char coded[]){    
      int count; 
      char letter;     
      for(count = 0;count < MAXLEN;count++){     
        letter = message[count];   
        if(isalpha(letter)){ 
          message[letter] = tolower(message[letter]); 
          message[letter] = secret[letter - 'a'];
          coded[letter] = message[letter]; 
        }    
      }  
    }

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, not replace message[count] with message[letter] - replace message[count] with letter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 10:29 AM
  2. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 12:04 PM

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