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; ok now that you say that...then my encrypt function is wrong. I need to take each character to see if ...

  1. #16
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    ok now that you say that...then my encrypt function is wrong. I need to take each character to see if it is a letter, then change it to lower case, then encrypt by mapping using 'a' as index 0 and so on. I don't think I am implemeenting it right and am stuck.

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    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)){
          letter = tolower(letter);  
          letter = secret[letter - 'a']; 
          coded[count] = letter; 
        }    
      }  
    }

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that's right.

    --
    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.

  4. #19
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    ok...so now my program should do what i want? but i am also getting a compilation error/warning for this line

    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)){
          letter = tolower(letter);  
          letter = secret[letter - 'a']; 
          coded[count] = letter; 
        }    
      }  
    }
    it is saying that the array subscript is of type 'char' ?

  5. #20
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    any thoughts

  6. #21
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    can't figure out the compilation error

    ok...so now my program should do what i want? but i am also getting a compilation error/warning for this line



    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)){
          letter = tolower(letter);  
          letter = secret[letter - 'a']; 
          coded[count] = letter; 
        }    
      }  
    }
    it is saying that the array subscript is of type 'char' ?

  7. #22
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Smile another try to please please check

    could I do something like this too


    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)){
           letter =tolower(letter);
        }    
        if (letter == 'a') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'b') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'c') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'd') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'e') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'f') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'g') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'h') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'i') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'j') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'k') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'l') 
            coded[count] = secret[letter - 'a'];
        else if (letter == 'm') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'n') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'o') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'p') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'q') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'r') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 's') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 't') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'u') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'v') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'w') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'x') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'y') 
            coded[count] = secret[letter - 'a'];
         else if (letter == 'z') 
            coded[count] = secret[letter - 'a']; 
      }  
    }

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Erm, that's kinda the same as:
    Code:
    coded[count] = secret[letter - 'a'];
    No IFs needed with that. OF course, I haven't read the thread from the beginning, but...

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Elysia, your code is not portable. (Neither was the OP's.) 'a' through 'z' are not guaranteed to be contiguous. ('0' through '9' are, however.)

    ok...so now my program should do what i want? but i am also getting a compilation error/warning for this line
    Code:
    if(isalpha(letter)){
    Be sure to #include <ctype.h>.

    Code:
        if(isalpha(letter)){
           letter =tolower(letter);
        }
    There's no need to use isalpha() -- tolower() handles alphabetic and non-alphabetic characters perfectly well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #25
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    check code

    so do i not need to use isalpha even though I don't want it to change the punctuation marks. I only want to change the letters to lower case and then encrypt them. the punctuation statys the same in the encryption. i do have the ctype.h included. so does what i have for the encryption function work with changing each alphabetic character??

  11. #26
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    fixed compiler error but now i need my code checked from above

    the alphabet code

  12. #27
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You need a final condition (a final "else") for all non-letters. Otherwise, array coded[] will have missing values.

    Todd

  13. #28
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by dwks View Post
    Elysia, your code is not portable. (Neither was the OP's.) 'a' through 'z' are not guaranteed to be contiguous. ('0' through '9' are, however.)
    Sure, but I was pointing out that that huge IF statement could be reduced to a single line.

  14. #29
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by dwks View Post
    Elysia, your code is not portable. (Neither was the OP's.) 'a' through 'z' are not guaranteed to be contiguous. ('0' through '9' are, however.)
    I think it's easy for us to get carried away with these beginning C students with too much minutia. You are exactly and precisely correct in pointing out that Elysia and the Original Poster are leveraging the ASCII character set and Latin-1 Code Page when checking for a-z in a contiguous sequence. However, in this intro to C class, I suspect they won't be porting their code to an IBM mainframe, converting it from ASCII to EBCDIC at the same time, nor will they being using Greek, Hebrew, German or Cyrillic characters (or otherwise deviating from Latin-1), when prompting the users for input, thus it will work just fine as designed.

    Todd

  15. #30
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You're certainly right, for the vast majority of systems, 'a' through 'z' are contiguous. Really, designing a system otherwise would be counter-intuitive.

    It's not that difficult to write code that doesn't break in the strange character sets where this is the case, however. An equivalent of this
    Code:
    coded[count] = secret[letter - 'a'];
    might be this:
    Code:
    const char *valid = "abcdefghiqklmnopqrstuvwxyz";
    const char *pos = strchr(valid, letter);
    if(pos) coded[count] = secret[valid - pos];
    And something like that is easily extensible, as well. You could easily add other characters to the valid string.

    You're probably right that the OP and his or her teacher don't really care, though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 2 of 2 FirstFirst 12
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, 09:29 AM
  2. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM

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