From d0784ec3b9f3d8b3881873ce81bcd67b01c0d137 Mon Sep 17 00:00:00 2001 From: Mo-ha-mm-ed <100417507+Mo-ha-mm-ed@users.noreply.github.com> Date: Fri, 30 Jun 2023 17:08:30 +0300 Subject: [PATCH] Update WhaleTalk.swift differently I made it differently and shorter and I thought it worth sharing --- 4-loops/whale-talk/WhaleTalk.swift | 149 +++++++++++------------------ 1 file changed, 55 insertions(+), 94 deletions(-) diff --git a/4-loops/whale-talk/WhaleTalk.swift b/4-loops/whale-talk/WhaleTalk.swift index ec6d244..1b095a8 100644 --- a/4-loops/whale-talk/WhaleTalk.swift +++ b/4-loops/whale-talk/WhaleTalk.swift @@ -1,99 +1,60 @@ -// Whale Talk πŸ‹ -// Kenny Lin - -var input = "I know not all that may be coming, but be it what it will, I'll go to it laughing. - Moby Dick" - -var output = "" - -for char in input { - let lowerChar = char.lowercased() - - switch lowerChar { - case "a", "i", "o": - output += lowerChar.uppercased() - case "e": - output += "EE" - case "u": - output += "UU" - default: - continue - } +// Whale Talk No.4 ===> final +// Date June 30, 2023 +// Swift Programming + +import Foundation + +//Take humantext +var input = "Turpentine and TurTlesπŸ«₯ !!!" + +//Output variable. Kepp it empty at the beginning +var output: String = NSLocalizedString(" ", comment: "") + +//Remove space from input and make it lowercase. +input = input.lowercased() +input = input.replacingOccurrences(of: " ", with: "") + +//Create for-in loop to iterate over input. +for charachter in input { + switch charachter { + + //Case 1: check if input has a, i, o + case "a", "i", "o": + //return a, i, o as they are. + output.append(charachter) + + //case 2 check if there is an "e". case 3 check if there is a "u".(Were seperated) + //Case 2 and 3 combined (this is part of Step 12 challenge 3, Final challenge) + case "e", "u": + output.append(String(repeating: charachter, count: 2)) + + //Case 4 (this is part of Step 12 challenge 1) + case let char where "!#$%&'()*+,-./:;<=>?@[]^_`{|}~".contains(char): + output.append(char) + + //Case 5 (this is part of Step 12 challenge 2) + case let char where "πŸ˜πŸŒ•β­οΈπŸ›οΈπŸ˜­πŸ˜’πŸ˜”β˜ΉοΈπŸ˜žπŸ™πŸ˜ͺπŸ˜ŸπŸ€’πŸŽ­πŸŽ­πŸ€―πŸ˜₯πŸ˜“πŸ«₯πŸ’©πŸ’€β˜ οΈπŸ‘ΎπŸ˜ΎπŸ˜ΏπŸ«¦πŸ¦·πŸ‘€βœπŸΌπŸ¦ΎπŸ¦ΏπŸ¦Ύ".contains(char): + output.append(char) + + + + //Finally other charachters, symbols, or constants are ignored. + default: + continue + } } -print(output) - -// Additional Challenges: -// First: -/* -var input = "I know not all that may be coming, but be it what it will, I'll go to it laughing. -Moby Dick" - -var output = "" - -for char in input { - let lowerChar = char.lowercased() - - switch lowerChar { - // Added "y" below: - case "a", "i", "o", "y": - output += lowerChar.uppercased() - case "e": - output += "EE" - case "u": - output += "UU" - default: - continue - } -} -print(output) -/* - -// Second: -/* -var input = "I know not all that may be coming, but be it what it will, I'll go to it laughing. -Moby Dick" - -var output = "" - -for char in input { - let lowerChar = char.lowercased() - - switch lowerChar { - // Added consonants: - case "a", "i", "o", "b", "p": - output += lowerChar.uppercased() - case "e": - output += "EE" - case "u": - output += "UU" - default: - continue - } -} +//Make output uppercase +output = output.uppercased() + +//Make output in whale language +print("Here is the translation of the human phrase \(input)") +print("in whale language ==> \(output) ") + +//Step 1-11 done + +//Step 12: Done above...... + -print(output) -/* - - -// Third: -/* -var input = "I know not all that may be coming, but be it what it will, I'll go to it laughing. -Moby Dick" - -var output = "" - -for char in input { - let lowerChar = char.lowercased() - - switch lowerChar { - case "a", "i", "o": - output += lowerChar.uppercased() - // Both "e" and "u" are in a single case - case "e", "u": - // Used a ternary operator to determine which letters get added - output += lowerChar == "e" ? "EE" : "UU" - default: - continue - } -} -print(output) -*/