Thread: Need help converting letters in a string

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    Need help converting letters in a string

    Hi, I have to write a program when a user enters 26 letters which is a code that corresponds the normal 26 letter (A-Z) in order and then enter their name which can be any where from 1 to 19 letters. Then I have to convert each letter from their name (which is a string) to the letter corresponding to the code they entered. Example:

    They enter for their code:
    ZYXWVUTSRQPONMLKJIHGFEDCBA

    Then enter their name:
    JAKE

    Then the program spits out their encoded name:
    QZPV

    I used an array to collect the 26 letters, then a string to collect the name, but how do I change each letter in the string to a new letter based on the array and then reprint it???

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    A string is just a special case of a character array, so you can simply swap indices between both array variables. What have you tried so far (i.e. code-wise)?

    Here's a link to a recent thread that might give you some clues, though won't give you an answer.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by Matticus View Post
    A string is just a special case of a character array, so you can simply swap indices between both array variables. What have you tried so far (i.e. code-wise)?

    Here's a link to a recent thread that might give you some clues, though won't give you an answer.
    There is some great info in that thread, HOWEVER I don't know how long my user's name is going to be or what their name is going to be. In that thread they are just picking and name and converting it which is simple.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    I am learning 'c' on my own and did the exercise.

    Just use fgets() for user input. C Style strings tutorial.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Jake Garbelotti View Post
    There is some great info in that thread, HOWEVER I don't know how long my user's name is going to be or what their name is going to be. In that thread they are just picking and name and converting it which is simple.
    Looking at that thread, I don't think it makes any difference. They use strlen(name), you can use strlen(name) too. If you have a 20 character array and read "JAKE" into it, strlen will tell you the string has length 4.

    "JAKE" is just a 4-character array of chars. So just process each char separately.

    Given 'J', you need to work out what letter that corresponds to in the code array. There's more than 1 way to do this -- you could do similarly to the other thread by looping, or you could devise a way of mapping each letter to 0-25 -- A=0,B=1 etc, then index the code array using that mapping. Advantages and disadvantages to either way, up to you to decide.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Jake, if you didn't HAVE a computer, how would you do this?

    Write out those small steps you would use. Taken together, they can be the "backbone" of your programs logic. If you don't see the pattern to the logic, do it a few times - or several times. Before long, you will see a pattern you use in solving this, yourself. That's the same pattern your program might use.

    Once you get that pattern understood, you can start the program. Post it up when you get that far, and tell us what problems you're having. We can be a lot more helpful after we can look at specific code.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by smokeyangel View Post
    Looking at that thread, I don't think it makes any difference. They use strlen(name), you can use strlen(name) too. If you have a 20 character array and read "JAKE" into it, strlen will tell you the string has length 4.

    "JAKE" is just a 4-character array of chars. So just process each char separately.

    Given 'J', you need to work out what letter that corresponds to in the code array. There's more than 1 way to do this -- you could do similarly to the other thread by looping, or you could devise a way of mapping each letter to 0-25 -- A=0,B=1 etc, then index the code array using that mapping. Advantages and disadvantages to either way, up to you to decide.
    The second way is the way I would like to do it. I just dont know how I can go from like name[0] being any letter to matching it with the proper letter and then assigning the new letter.

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    This is the start I have so far:

    Code:
    //included libraries#include <stdio.h>
    #include <math.h>
    
    
    //Main Function
    int main() {
        //Most of your program should go here.
    
    
        //Declare strings and arrays
        int i,j;
        char name[18];
        char cipher[25];
        char alphabet[25] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    
    
        //Ask for the cipher
        printf("What is your substitution cipher?\n");
    
    
        //Scan for the cipher
        for(i=0; i<26; i++){
            scanf("%c", &cipher[i]);
        }
    
    
        //Ask for the users name
        printf("What is your name?\n");
        scanf("%s", &name);
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Jake Garbelotti View Post
    This is the start I have so far:

    Code:
    //included libraries
    #include <stdio.h>
    #include <math.h> //is this needed?
    
    
    //Main Function
    int main() {
        //Most of your program should go here.
    
    
        //Declare strings and arrays
        int i,j;
        char name[18];
        char cipher[25];
        char alphabet[25] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    
        /* 
        Note that the alphabet has 26 letters, not 25. A simpler way:
        char alphabet[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
        Now alphabet[] will have 27 spaces. 26 for the letters, and one    
        more for the end of string char: '\0', which you can't see here, but 
        is required for string functions, in C.
       */
    
    
        //Ask for the cipher
        printf("What is your substitution cipher?\n");
    
    
        //Scan for the cipher
        for(i=0; i<26; i++){
            scanf("%c", &cipher[i]);
        }
    
        /* why not the simpler
        scanf("%s", cipher); ?
        */
    
    
        //Ask for the users name
        printf("What is your name?\n");
        scanf("%s", &name);
        /* correct is:
        scanf("%s", name);
        the name of the array, IS the address to the array's first element
       */
    
        return 0;
    }
    The above is all syntax stuff. Now, how would you solve this puzzle? No code needed for this part,yet. Pure puzzle solving! Of course, you can do it, but can you see the WAY that you solve it - the pattern of logic you use to do that?

    You can use that same pattern of logic, in your program.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Jake Garbelotti View Post
    The second way is the way I would like to do it. I just dont know how I can go from like name[0] being any letter to matching it with the proper letter and then assigning the new letter.
    Just think about how you'd do it yourself (as a person not a computer). Let's say you want to substitute 'J'. You figure out that J is the 10th letter in the alphabet, so you pick the 10th cipher letter.

    Characters in C have an underlying representation as a number. For example, ASCII: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
    (Of course ASCII isn't the only character encoding in use, but for a program like this I think it's fair to assume everything will be in ASCII. Just remember that that's not something you can universally assume with all programs. )

    Code:
    int i = 'C';     // i is 67
    char c = 86; // c is 60, can be written 'V'
    If the value of 'A' was 0, 'B' 1 and so on, it'd be really easy, right? You could just use the letter itself to index into the cipher array

    Code:
       coded_char = cipher[name[i]];
    It's not quite that easy - if you look at the ASCII values you can see that A=65,B=66 and so on. So you'd need to map these to 0,1 before indexing the array, by subtracting 65. A starting point would probably be to try to map the name onto your alphabet array-- then once you get back the same name you input, you can just swap in the cipher array.

    That's how I'd do it anyway. You need to be careful to check that the input letter is valid, or you'll access outside the array and crash your program (probably). Likewise if you put in lowercase characters -- these have different encodings to uppercase, so you need to handle that.

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    Thanks for the help, and I understand how it works, like J should change to the 10th letter of the cipher string, I just have no idea how to assign J a number and change it to the corresponding value before printing the name again. Do I have to use a new string for the new name???

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Whatever suits your needs. You can create a new string, or write over the old one, or just print the characters as you encode them. Examples below:

    Code:
    for (i = 0; i < strlen(name); i++)
    {
         char c = name[i]; // get the letter
         // use the value of 'c' to work out an index num between 0-25
         putc(cipher[index]); // print character 
         printf("%c", cipher[index]); // alternative way of printing character
         newname[i] = cipher[index]; // write the coded name into another buffer
         name[i] = cipher[index]; // write over the old string
    
    }
    For getting the index into the array you just subtract the value of 'A' from the letter. Because 'A' is 65 and you want to access element 0 of cipher for 'A': 65-65=0. Then B is 66... 66-65=1

    Code:
    int index = name[i] - 'A'; // or 
    int index = name[i] - 65;  //if you find it clearer
    Breaking it down a bit using a couple of temporary variables:

    Code:
    char plain_char = name[i];
    int index = plain_char - 65;
    char coded_char = cipher[index]
    Before I forget:
    Quote Originally Posted by Jake Garbelotti
    Code:
        char cipher[25];
        char alphabet[25] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    Your arrays are too small, only 25 elements and you need 26. If you decide to use scanf to get the cipher string then you'll need 27 -- 26 characters and the null terminator at the end of the string.

    Ah - actually I see Adak already said that.


    I suggest you start by just writing a loop that prints out the original name without trying to encode it. Get that compiled and working, then start trying to work on the cipher part. I think from the code you posted that you haven't actually tried to compile it yet (it wouldn't compiler) -- it's best to be compiling and testing as you go -- the more untested code you have to debug at the end the harder it'll be for you to spot mistakes.

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    Thanks you so much for your help! I now have to code working perfectly and your help has greatly helped me understand what is going on with strings. Thanks once again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-03-2010, 08:47 AM
  2. converting letters into numbers.
    By xforevertink in forum C++ Programming
    Replies: 8
    Last Post: 07-20-2009, 12:12 AM
  3. converting numbers to letters
    By sh4k3 in forum C Programming
    Replies: 6
    Last Post: 06-10-2007, 07:15 PM
  4. Replies: 4
    Last Post: 04-19-2005, 08:05 PM
  5. converting numbers to letters??
    By Lau in forum C Programming
    Replies: 5
    Last Post: 11-06-2002, 12:17 PM