Advertisement
jamesdylangoldstein

VigenereRepeatSwift

Jun 13th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.36 KB | None | 0 0
  1. //
  2. //  main.swift
  3. //  VigenereSwift
  4. //
  5. //  Created by James Dylan Goldstein on 6/13/16.
  6. //  Copyright © 2016 James Dylan Goldstein. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11.  
  12. // Prebuilt function that allows for the use of subscripts in strings
  13. // http://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language
  14. extension String
  15. {
  16.    
  17.     subscript (i: Int) -> Character {
  18.         return self[self.startIndex.advancedBy(i)]
  19.     }
  20.    
  21.     subscript (i: Int) -> String {
  22.         return String(self[i] as Character)
  23.     }
  24.    
  25.     subscript (r: Range<Int>) -> String {
  26.         let start = startIndex.advancedBy(r.startIndex)
  27.         let end = start.advancedBy(r.endIndex - r.startIndex)
  28.         return self[Range(start ..< end)]
  29.     }
  30. }
  31.  
  32. // Function to obtain a string that will be encoded using Vigenere's Cipher
  33. func obtainSentenceToEncode() -> String
  34. {
  35.     print("Type the sentence you would like to encode")
  36.     let sentenceToEncode = readLine(stripNewline: true)
  37.     return sentenceToEncode!
  38. }
  39.  
  40. // Function to repeat the cipher word over and over
  41. func repeatCipherWord(cipherWord: String, sentenceToEncode: String) -> String
  42. {
  43.    
  44.     var cipherWordRepeated = ""
  45.     var endOfWordCounter = 0
  46.     let sentenceLength = sentenceToEncode.characters.count
  47.     let cipherWordLength = cipherWord.characters.count
  48.     print("Scrambled word pass in as command line argument is: \(cipherWord)")
  49.    
  50.     for counter in 0..<sentenceLength
  51.     {
  52.         if((sentenceToEncode[counter] >= Character(UnicodeScalar(97)) && sentenceToEncode[counter] <= Character(UnicodeScalar(122))) || (sentenceToEncode[counter] >= Character(UnicodeScalar(65)) && sentenceToEncode[counter] <= Character(UnicodeScalar(90))))
  53.         {
  54.             cipherWordRepeated += cipherWord[endOfWordCounter]
  55.        
  56.             endOfWordCounter += 1;
  57.             if (endOfWordCounter >= cipherWordLength)
  58.             {
  59.                 endOfWordCounter = 0;
  60.             }
  61.         }
  62.         else
  63.         {
  64.             cipherWordRepeated += " "
  65.         }
  66.     }
  67.    
  68.     return cipherWordRepeated
  69. }
  70.  
  71. var cipherWord = (Process.arguments[1])
  72.  
  73. let sentenceToEncode = obtainSentenceToEncode()
  74.  
  75. print("\(sentenceToEncode)")
  76.  
  77. var cipherWordRepeated = repeatCipherWord(cipherWord, sentenceToEncode: sentenceToEncode)
  78.  
  79. print(cipherWordRepeated)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement