Advertisement
jamesdylangoldstein

VigenereNoErrorsBadExec

Jun 13th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. // This code generates Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. char* CipherRepeater(char* sentencetoencode, char* cipherwordtorepeat, char* ciphersentence);
  7.  
  8. int main(int argc, char * argv[])
  9. {
  10.     // This is a message to encode using Vigenere cipher
  11.     char sentencetoencode[100] = "secret message is come over at five";
  12.    
  13.     // Pass a word at command line, then assign that word to cipherwordtorepeat
  14.     // This word is the Vigenere encoding key
  15.     // Example word: cat
  16.     char cipherwordtorepeat[100];
  17.    
  18.     for(unsigned long argvcounter = 0, argvlength = strlen(argv[1]); argvcounter < argvlength; argvcounter++)
  19.     {
  20.         cipherwordtorepeat[argvcounter] = argv[1][argvcounter];
  21.     }
  22.    
  23.     // Now the word cat needs to be repeated until it matches the length of the message
  24.     // Example "secret message is come over at five"
  25.     //         "catcat catcatc at catc atca tc atca"
  26.     // Declare an array of characters
  27.     // Then the function fills the array with the repeating word
  28.     char ciphersentence[100];
  29.     CipherRepeater(sentencetoencode, cipherwordtorepeat, ciphersentence);
  30.    
  31.     printf("%s\n", ciphersentence);
  32.    
  33.     return 0;
  34. }
  35.  
  36. char* CipherRepeater(char* sentencetoencode, char* cipherwordtorepeat, char* ciphersentence)
  37. {
  38.     // Create a counter to make sure to go back to char 0 in the word array when end is reached
  39.     int endofwordcounter = 0;
  40.     unsigned long cipherwordlength = strlen(cipherwordtorepeat);
  41.    
  42.     // Take the cipher word and repeat it until it's the same length as the word to encode
  43.     for (unsigned long repeatciphercounter = 0, sentencetoencodelength = strlen(sentencetoencode); repeatciphercounter < sentencetoencodelength; repeatciphercounter++)
  44.     {
  45.         // Put a space in the new string in any spot where a non-letter is used
  46.         if ((int)sentencetoencode[repeatciphercounter] < 65 || ((int)&sentencetoencode[repeatciphercounter] > 90 && (int)sentencetoencode[repeatciphercounter] < 97) || (int)sentencetoencode[repeatciphercounter] > 122)
  47.         {
  48.             ciphersentence[repeatciphercounter] = (char)32;
  49.         }
  50.         else
  51.         {
  52.             // Copy the character
  53.             ciphersentence[repeatciphercounter] = cipherwordtorepeat[endofwordcounter];
  54.             endofwordcounter++;
  55.             if (endofwordcounter >= cipherwordlength)
  56.             {
  57.                 endofwordcounter = 0;
  58.             }
  59.         }
  60.     }
  61.    
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement