Giter Site home page Giter Site logo

ios-strings-lab-2's Introduction

Strings Lab 2

Fork and clone this repo. On your fork, answer and commit the follow questions. When you are finished, submit the link to your repo on Canvas.

Question 1

You are given a string stored in variable problem. Write code so that you print each word of the string on a new line.

var problem = "split this string into words and print them on separate lines"

var problem2 = problem.components(separatedBy: " ")

for i in problem2 {
print (String(i))
}

Example

Input:
`var problem ="split this string into words and print them on separate lines"`

Output:
```swift
split
this
string
into
words
and
print
them
on
separate
lines

Question 2

Given a string testString create a new variable called condensedString that has any consecutive spaces in testString replaced with a single space.

let testString = "  How   about      thesespaces  ?  "
//condensedString = " How about thesespaces ? "

et condensedString = aString.replacingOccurrences (of: "  ", with: " ", options: .literal, range: nil)

l

Question 3

Given a string with multiple words. Reverse the string word by word.

Example:

Sample Input: "Swift is the best language"

Sample Output: "language best the is Swift"

var question3 = "Swift is the best launguage"

var words = question3.split(separator: " ")

print(words)

for _ in words {
words.reverse()
}
print(words)

Question 4

Given a string with multiple words. Write code that prints how many of them are palindromes.

Example:

Sample Input: "danaerys dad cat civic bottle"

Sample Output: 2


var question4 = "danaerys dad cat civic bottle"

var words = question4.split(separator: " ")
var palindromes = 0

for i in words {
if String(i.reversed()) == String(i) {
palindromes += 1
}
}
print(palindromes)

Question 5

You are given a string representing an attendance record for a student. The record only contains the following three characters:

'A' : Absent.

'L' : Late.

'P' : Present.

If a student has more than one 'A' or more than 2 continuous 'L's that student should not be rewarded. Print true if student is to be rewarded and False if they shouldn't.

Example:

Sample Input: "PPALLP"

Sample Output: true

let attendance = "PPALLP"
var howManyAs = ""
var howManyLs = "LLL"

for i in attendance{
if i == "A"{
howManyAs += String(i)
}
}
if howManyAs.count > 1 || howManyLs.contains(howManyLs)  {
print (false)
}else {
print (true)
}

Question 6

Given a tuple with two strings. The first string is a ransom note, the second string being the characters from a magazine. Determine whether or not you can construct the ransom note using the characters from the magazine.

Each letter from the magazine can only be used once. You can assume all letters are lowercased.

Examples:

Sample Input1: ("a", "b")

Sample Output1: False

Sample Input2: ("aa", "aab")

Sample Output2: True


var aTuple = ("magazineLetters", "ransomNote")
var aBool = false
var count = 0

for i in aTuple.0 { //h, i
for j in aTuple.1 { //i
if j == i {
count += 1
continue
} else {
continue
}
}
}
if count == (aTuple.0).count {
aBool = true
}

print(aBool)

ios-strings-lab-2's People

Contributors

davidlawrencer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.