Thread: Indexing an Array, Based on Char Values from another.

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    2

    Indexing an Array, Based on Char Values from another.

    Hello, I am trying to make a substitution cipher for class. I feel strongly about how I approached the code, but am new to arrays and am having trouble getting indexes to stick during my loop action.

    I have been struggling to find out how I can successfully declare that an empty array be filled with pieces from another array that were matched during the loop.

    The purpose of this code is to change a test name into an encoded name based on a substitution cipher using arrays and strings.

    Whenever I run this code, printing the secret[] array which should house the encoded name results in odd symbols. I am clearly doing something wrong, but aside from how I am trying to jam values from the cipher array into the secret array I cant imagine why.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    //Based on 4 1D Arrays;
    
    //alphabet[] is meant to store the regular alphabet.
    //cipher[] is meant to store the cipher.
    //name[] is meant to store the test name.
    //secret[] is meant to store the final encoded result
    
    
    int main() {
    
    char cipher[30], alphabet[30], name[30], secret[30];
    
    int i=0, j=0, length;
    
    
    //Filling in the first three arrays
    
    //The Alphabet (26 letters)
    
    strcpy(alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");  
    
    //The Substituted Cipher for the Alphabet (26 letters). 
    
    strcpy(cipher, "QAZWSXEDCRFVTGBYHNUJMIKOLP");  
    
    //The Test name. 
    
    strcpy(name, "SARAH"); 
    
    
    //Testing the strlen tool
    
    length = strlen(name);
    
    printf("\n The length of the name is %d\n", length);
    
    
    //The Main Loop should go on until every letter in the test name is covered.
    
    for(i=0; j=0; i < strlen(name)) {
    
    //If the current letter of name and the current letter of alphabet do match:                             
      
    if (name[i] = alphabet[j]) {  
    
    //Copy the letter of cipher that represents the matching alphabetical letter into the current spot of secret, which holds the encoded name.    
       
    strcpy(secret[i], cipher[j]);  
    
    //Move on to the next letter of the name. 
    
    i = i++ ;   
      
    //Reset Alphabetical tracking.              
    
    j = 0 ; 
    
    }
    
    //Else if the current letter of name and the current letter of alphabet do not match:
    
    else    
     
    //move on to the next letter of alphabet.
    
    j = j++;  
    
    }
    
    //Once all letters of the name are covered:
    
    if (i = strlen(name)) {  
    
    //Print the encoded name from the secret[] array.
    
    printf("\n Your encoded name is %s\n", secret);      
    
    //Printing the other 3 rows to check for errors.
    
    printf("\n Alphabet is %s\n", alphabet);
    printf("\n Cipher is %s\n", cipher);
    printf("\n Real name is %s\n", name);
    
    }
    
    //Current Issue: secret does not come out as intended, wondering if the inputs are happening as they should.
    
    return 0;
    
    }
    There is probably more writing than code in this, but I felt it important to state exactly what I thought I was doing. If anyone could point out if I am missing anything obvious, it would be greatly appreciated. I simply don't know how to crack this nut.
    Attached Files Attached Files

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There's a lot of mistakes in your code. I've added comments to it, starting with //!!
    Code:
    #include <stdio.h>
    #include <string.h>
    
    //Based on 4 1D Arrays;
    
    //alphabet[] is meant to store the regular alphabet.
    //cipher[] is meant to store the cipher.
    //name[] is meant to store the test name.
    //secret[] is meant to store the final encoded result
    
    
    int main() {
      char cipher[30], alphabet[30], name[30], secret[30];
      int i=0, j=0, length;
    
      //Filling in the first three arrays
    
      //The Alphabet (26 letters)
      strcpy(alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");  
    
      //The Substituted Cipher for the Alphabet (26 letters). 
      strcpy(cipher, "QAZWSXEDCRFVTGBYHNUJMIKOLP");  
    
      //The Test name. 
      strcpy(name, "SARAH"); 
    
      //Testing the strlen tool
      length = strlen(name);
    
      printf("\n The length of the name is %d\n", length);
    
      //The Main Loop should go on until every letter in the test name is covered.
    
      for(i=0; j=0; i < strlen(name)) {  //!! Should be more like  for (i=0, j=0; i < strlen(name); )
        //If the current letter of name and the current letter of alphabet do match:                             
    
        if (name[i] = alphabet[j]) {  //!! To test for equality, use ==
    
          //Copy the letter of cipher that represents the matching alphabetical letter into the current spot of secret, which holds the encoded name.    
          strcpy(secret[i], cipher[j]);   //!! To copy a single char, just use secret[i] = cipher[j]
    
          //Move on to the next letter of the name. 
          i = i++ ;   //!! No! Just use i++
      
          //Reset Alphabetical tracking.              
          j = 0 ;
        }
        //Else if the current letter of name and the current letter of alphabet do not match:
        else    
        //move on to the next letter of alphabet.
          j = j++;  //!! Again, no. Just j++
      }
    
      //Once all letters of the name are covered:
      if (i = strlen(name)) {  //!! To test for equality, use ==
    
        //Print the encoded name from the secret[] array.
        printf("\n Your encoded name is %s\n", secret);      
    
        //Printing the other 3 rows to check for errors.
        printf("\n Alphabet is %s\n", alphabet);
        printf("\n Cipher is %s\n", cipher);
        printf("\n Real name is %s\n", name);
      }
    
      //Current Issue: secret does not come out as intended, wondering if the inputs are happening as they should.
      return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The problem lies in this section here:

    Code:
    for(i=0; j=0; i < strlen(name))
    {
        //If the current letter of name and the current letter of alphabet do match:                                
        if (name[i] = alphabet[j])
        {
            //Copy the letter of cipher that represents the matching alphabetical letter into the current spot of secret, which holds the encoded name.    
            strcpy(secret[i], cipher[j]);
            //Move on to the next letter of the name. 
            i = i++ ;
            //Reset Alphabetical tracking.
            j = 0 ; 
        }
        //Else if the current letter of name and the current letter of alphabet do not match:
        else
            //move on to the next letter of alphabet.
            j = j++;  
    }
    The arguments in your "for" loop are not correct (the third argument is the one performed each iteration of the loop ... in this case, it doesn't do anything)

    Code:
    for(i=0; j=0; i < strlen(name))
    Your "if" statement is using the assignment operator (=) instead of the comparison operator (==)

    Code:
    if (name[i] = alphabet[j])
    If you're going through it letter by letter, then you can just assign the value of the curernt "cipher" element to the current "secret" element - you should not be using "strcpy" for this

    Code:
    strcpy(secret[i], cipher[j]);  // <--- wrong
    secret[i] = cipher[j];  // <--- try this
    ---

    [edit] oogabooga was a lot more thorough than I - pay more attention to that post.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    2
    Wonderful! I had the completely wrong idea as to what was wrong with my work. Applying the fixes both of you have recommended, I have gotten my code to largely work!

    Thank you very much Oogabooga and Matticus!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. values into array based on coordinates
    By BabyWasabi in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2006, 07:48 PM
  2. Converting Char Array Loop values to integer
    By azamsharp1 in forum C Programming
    Replies: 8
    Last Post: 10-27-2005, 09:13 AM
  3. fill unsigned char array with hex values
    By luigi40 in forum C Programming
    Replies: 1
    Last Post: 06-25-2005, 05:56 AM
  4. Comparing char values in an array using 'if'
    By Judy_528 in forum C++ Programming
    Replies: 18
    Last Post: 04-25-2002, 07:52 AM
  5. array indexing
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-01-2001, 11:42 AM