Advertisement
jamesdylangoldstein

VigenereImplementingFunctions

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