2014年8月10日日曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 7(Using Methods)、7.6(Exercises) 1-a, b, c, d, e, f, g, h, i.をSwiftで考えてみる。

7.6(Exercises) 1-a, b, c, d, e, f, g, h, i.

コード(Xcode)

string.swift

//
//  string.swift
//  string
//
//  Created by kamimura on 8/10/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

extension String {
    var swapcaseString:String {
    var result: String = ""
        for ch in self {
            let s = String(ch)
            result += s.uppercaseString == s ? s.lowercaseString : s.uppercaseString
        }
        return result
    }
    var isUpper:Bool { return self.uppercaseString == self }
    func count(sub:String) -> Int {
        var result:Int = 0
        var s = self
        let index:String.Index = "a".endIndex
        while s != "" {
            if s.hasPrefix(sub) {
                result += 1
                s = s.substringFromIndex(sub.endIndex)
            } else {
                s = s.substringFromIndex(index)
            }
            
        }
        return result
    }
}

main.swift

//
//  main.swift
//  string
//
//  Created by kamimura on 8/10/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

println("Hello, World!")

println("hello".uppercaseString)

println("Happy Birth!".lowercaseString)

println("WeeeEEEEeeeEEEEeee".swapcaseString)

println("ABC123".isUpper)

println("aeiouAEIOU".count("a"))

println("hello".hasSuffix("o"))

println("hello".hasPrefix("H"))

let s = "Swift"
println("Hello \(s)")

let s1 = "World"
println("Hello \(s)! Hello \(s1)!")

入出力結果(Console Output)

Hello, World!
HELLO
happy birth!
wEEEeeeeEEEeeeeEEE
true
1
true
false
Hello Swift
Hello Swift! Hello World!
Program ended with exit code: 0

0 コメント:

コメントを投稿