Thread: Abracadabra!

  1. #1
    Registered User tacosrule's Avatar
    Join Date
    Nov 2008
    Posts
    4

    Abracadabra!

    So I'm a total noob to C programming and I've got no idea how to make this program. I've started on it, but I'm still completely lost. It's an entry level C programming class that I'm in so it must not be too complicated, but I'm coming up blank.

    Basically, I need to make a substitution cipher that allows me to input an encryption key and then have that encryption key encrypt a seven letter word, but only after I've made sure that the word is within the ABRACADABRA alphabet.

    The assignment is problem 3, found at http://ece15.ucsd.edu/Labs/lab2.pdf .

    Here's what I've written so far. All I'm trying to do is compare one array to another array to make sure that every letter in my "word" array matches one of the letters in my "natural" array (that has the ABRACADABRA alphabet in it). I haven't even started on the encryption part yet, but I think I have the algorithm worked out for that.

    Code:
    int main() 
    {
      int i=0,w=0,j=0;
      char nat[5]= {'a','b','c','d','r'};
      char in[5], inw[7];  
    
      printf("Enter key:  ");
    
      for(i=0;i<5;i++)
        {
        in[i] = getchar();
        }
    
      printf("Enter word: ");
      
      for(w=0;w<7;w++)
        {
          inw[w] = getchar();
        }
      
      while(j<5)
        {
          while(inw[w] != nat[j])
    	{
    	  j++;
    	 }
          if(inw[w] == nat[j])
    	{
          w++;
          j=0;
    	}
        }
    
    
      if(j == 5)
        {
          printf("You did not speak ABRACADABRA to me!\n");
        }
    	
      
      return 0;
    }
    
    //else if(w ==7)
    You can see I barely started to work on the next part of the problem, but I need to figure out how to do this check first...
    Agh! So frustrating! I don't expect anyone to do my homework for me, just some hints or some pointers would help me a LOT.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    OK. Lets start from your while loops. In order to see if what you though is right you should always check one by one iteration. So:
    Code:
      while(j<5)
      {
          while(inw[w] != nat[j])
          {
    	  j++;
          }
          if(inw[w] == nat[j])
          {
              w++;
              j=0;
          }
      }
    So first will the "while(inw[w] != nat[j])" code executed since j ==0 so j <5.
    Now, you expect this loop to stop when j == 5. And you are wrong. When you have one while-loop inside the other the inner loop is executed first. So the "while(inw[w] != nat[j])" will execute and stop ONLY if inw[w] != nat[j]. But what happens if it is not met and j increases more than 5? Then you will read something like nat[6] which is invalid. What you want is this:
    Code:
    while(inw[w] != nat[j] && j <5) {...}
    with the AND (&&) both conditions have to be met in order for the loop to be executed.

    So fix the while loop so it does what you want. You want to check every inw[] with every nat[]. So you can simply use a double for loop for this thing. I ll get you started:
    Code:
    for (i = 0; i < 7; ++i)
    {
       for (j =0; j < 5; ++j)
       {
          //your code
       }
    }
    Can you finish this loop?

  3. #3
    Registered User tacosrule's Avatar
    Join Date
    Nov 2008
    Posts
    4
    Ok I'll try that. Thanks!

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    You'll also need to eat the newline after reading the first five characters. So after your first for-loop you'll need an extra getchar(). Actually, there are better ways to read strings, but maybe you were instructed to do it this way.

  5. #5
    Registered User tacosrule's Avatar
    Join Date
    Nov 2008
    Posts
    4
    Would it be better to use
    Code:
    scanf("%s", in);
    to input the encryption key?
    I need to limit the key to five characters, so is that possible to do with scanf?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Code:
    scanf("%5s", in);

  7. #7
    Registered User tacosrule's Avatar
    Join Date
    Nov 2008
    Posts
    4
    Here's what I ended up doing:
    Code:
      for(w=0;w<7;w++)
        {
          for(j=0;j<5;j++)
    	{
    	  if(inw[w] == nat[j])
    	    {
    	      i=j;
    	      enc[c]=in[i];
    	      c++;
    	      j=0;
    	      break;
    	    }
    	}
    
       if(j==5)
    	{
    	  printf("You did not speak ABRACADABRA to me!\n");
    	  return 1;
    	}
        }
    (That has the encryption coding in it too)

    Thanks for your help!

Popular pages Recent additions subscribe to a feed