Last active
October 15, 2019 22:47
-
-
Save ericlmartinezo/8fb7d597111aeb10f85768e0d3f1cb79 to your computer and use it in GitHub Desktop.
Stupid code paring test 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Counter the number of times a specific word is repeated in a sentence | |
// Created a variable with the phrase | |
var phrase = "The weather is good today. The weather was worse yesterday. The the weather tomorrow is better." | |
// Created funtion to be call that will take a message and the word that I want to find the number of time it repeats | |
// The function will return an Int | |
func SpecificWordCount(str:String, word:String) ->Int { | |
// The logic below looks into the components of the phrase for the input word separated by a space. | |
// a variable count is add to hold the values | |
let words = phrase.components(separatedBy: " "); var count = 0 | |
// The for in loop will help irriate through the phrase's find the input character for each one found it will add 1 to the variable | |
// count | |
for thing in words { | |
if thing == word { | |
count += 1 | |
} | |
} | |
// Since its function Swift wants me to return something so I will return the total count to the runtime console | |
return count | |
} | |
// There's two ways I can run the function | |
// 1. Creating a variable to then printing it by Passing the word you want to count with a nice message | |
var pritnResults = SpecificWordCount(str: "Print number of times word is used", word: "The") | |
print(pritnResults) | |
// Or by calling the funtion | |
SpecificWordCount(str: "Print number of times word is used", word: "The") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment