Skip to content

Instantly share code, notes, and snippets.

@sooop

sooop/hangulSound.h

Last active Dec 13, 2015
Embed
What would you like to do?
[Objective-C] 한글 초성, 중성, 종성을 분리해주는 클래스
//
// hangulSound.h
// firstcode
//
// Created by BONGSOO KWON on 12. 1. 31..
// Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface hangulSound : NSObject
+(NSString *)firstSoundWithString:(NSString *)hangul;
+(NSString *)middleSoundWithString:(NSString *)hangul;
+(NSString *)lastSoundWithString:(NSString *)hangul;
@end
//
// hangulSound.m
// firstcode
//
// Created by BONGSOO KWON on 12. 1. 31..
// Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//
#import "hangulSound.h"
@implementation hangulSound
+(NSString *)initSoundWithString:(NSString *)hangul
{
NSString *result = @"";
int i;
for (i=0; i<[hangul length]; i++) {
unichar oneChar = [hangul characterAtIndex:i];
if (oneChar >= 0xAC00 && oneChar <= 0xD7A3) {
int firstCodeValue = ((oneChar - 0xAC00)/28)/21;
firstCodeValue += 0x1100;
result = [result stringByAppendingString:
[NSString stringWithFormat:@"%C", firstCodeValue]];
}
}
return result;
}
+(NSString *)middleSoundWithString:(NSString *)hangul
{
NSString *result = @"";
int i;
for (i=0; i<[hangul length]; i++) {
unichar oneChar = [hangul characterAtIndex:i];
if (oneChar >= 0xAC00 && oneChar <= 0xD7A3) {
int firstCodeValue = ((oneChar - 0xAC00)/28)%21;
firstCodeValue += 0x1161;
result = [result stringByAppendingString:
[NSString stringWithFormat:@"%C", firstCodeValue]];
}
}
return result;
}
+(NSString *)lastSoundWithString:(id)hangul
{
NSString *result = @"";
int i;
for (i=0; i<[hangul length]; i++) {
unichar oneChar = [hangul characterAtIndex:i];
if (oneChar >= 0xAC00 && oneChar <= 0xD7A3) {
int firstCodeValue = (oneChar - 0xAC00)%28;
firstCodeValue += 0x11A7;
result = [result stringByAppendingString:
[NSString stringWithFormat:@"%C", firstCodeValue]];
}
}
return result;
}
@end
//
// main.m
// firstcode
//
// Created by BONGSOO KWON on 12. 1. 31..
// Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "hangulSound.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
// argc는 파라미터의 개수를 의미. 항상 1보다 크다. (첫 번째 파라미터는 파일 자신의 경로)
if (argc < 2) {
// 파라미터가 들어오지 않았음
printf("USAGE : firstcode <str>\n");
} else {
NSString *result = @"";
for (int i=1;i<argc;i++){
result = [NSString stringWithFormat:@"%@%@", result,
[NSString stringWithUTF8String:argv[i]]];
}
result = [hangulSound firstSoundWithString:result];
printf("%s\n",[result UTF8String]);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.