2014年8月15日金曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の2.5章(文字列: 文字列理論)、プールパズル(p.90)をSwiftで考えてみる。

プールパズル(p.90)

コード(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 }
    var isLower:Bool { return self.lowercaseString == 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
    }
    func find(sub:String, start:Int = 0, end:Int? = nil) -> Int {
        var s = self as NSString
        var s_temp = s.substringFromIndex(start)
        let end_temp:Int = end ? end! : s.length
        for i in start..<end_temp {
            if (s_temp.hasPrefix(sub)) {
                return i
            }
            s_temp = s.substringFromIndex(i + 1) as NSString
        }
        return -1
    }
    func replace(old_str:String, new_str:String) -> String {
        return self.stringByReplacingOccurrencesOfString(old_str, withString: new_str)
    }
    func strip() -> String {
        return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    }
    func slice(start:Int=0, end:Int?=nil) -> String {
        let s = self as NSString
        var new_end:Int
        if (end) {
            new_end = end!
        } else {
            new_end = s.length
        }
        return  s.substringWithRange(NSRange(location: start, length:new_end - start))
    }
}

main.swift

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

import Foundation

let tracks:[String] = [
    "I left my heart in Harvard Med School",
    "Newark, Newark - a wonderful town",
    "Dancing with a Dork",
    "From here to maternity",
    "The girl from Iwo Jima"
]

func findTrack(search_for:String) {
    for i in 0..<tracks.count {
        if tracks[i].find(search_for) != -1 {
            println("曲番号\(i): \(tracks[i])")
        }
    }
}

// 標準入力(STDIN)から読み込む
func input(msg:String = "") -> String {
    print(msg)
    var in_fh = NSFileHandle.fileHandleWithStandardInput()
    var data = in_fh.availableData
    var s = NSString(data: data, encoding: NSUTF8StringEncoding)
    s = s.substringToIndex(s.length - 1)
    return s
}

while true {
    print("曲名の一部を入力: ")
    let search_for = input()
    if search_for == "q" {
        break
    }
    findTrack(search_for)
}

入出力結果(Console Output)

曲名の一部を入力: left
曲番号0: I left my heart in Harvard Med School
曲名の一部を入力: -
曲番号1: Newark, Newark - a wonderful town
曲名の一部を入力: with
曲番号2: Dancing with a Dork
曲名の一部を入力: to
曲番号1: Newark, Newark - a wonderful town
曲番号3: From here to maternity
曲名の一部を入力: abcde
曲名の一部を入力: q
Program ended with exit code: 0

0 コメント:

コメントを投稿