Skip to content

Instantly share code, notes, and snippets.

@sooop
Last active March 8, 2021 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sooop/4958930 to your computer and use it in GitHub Desktop.
Save sooop/4958930 to your computer and use it in GitHub Desktop.
[Objective-C] 한글 초성, 중성, 종성을 분리해주는 클래스
//
// hangulSound.h
// firstcode
//
// Created by sooop on 12. 1. 31..
// Copyright (c) 2012년 soooprmx.com All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HangulSound : NSObject
+(NSString *)choseongWithString:(NSString *)text;
+(NSString *)jungseongWithString:(NSString *)text;
+(NSString *)jongseongWithString:(NSString *)text;
@end
//
// hangulSound.m
// firstcode
//
// Created by sooop on 12. 1. 31..
// Copyright (c) 2012년 soooprmx.com All rights reserved.
//
#import "HangulSound.h"
@implementation HangulSound
+(NSString*)choseongWithString:(NSString*)text;
{
NSMutableArray<NSString*> * array = [NSMutableArray arrayWithCapacity:[text length]];
int i;
unichar oneChar, target;
NSString *result;
@autoreleasepool {
for (i=0; i<[text length]; i++) {
oneChar = [hangul characterAtIndex:i];
if (oneChar >= 0xAC00 && oneChar <= 0xD7A3) {
target = ((oneChar - 0xAC00)/28)/21 += 0x1100;
result = [NSString stringWithFormat:@"%C", target];
} else {
result = [NSString stringWithFormat:@"%C", oneChar];
}
[array addObject:result];
}
}
return [array componentsJoinedByString:@""];
}
+(NSString*)jungseongWithString:(NSString*)text
{
NSMutableArray<NSString*> * array = [NSMutableArray arrayWithCapacity:[text length]];
int i;
unichar oneChar, target;
NSString *result;
@autoreleasepool {
for (i=0; i<[text length]; i++) {
oneChar = [hangul characterAtIndex:i];
if (oneChar >= 0xAC00 && oneChar <= 0xD7A3) {
target = ((oneChar - 0xAC00)/28)%21 += 0x1161;
result = [NSString stringWithFormat:@"%C", target];
} else {
result = [NSString stringWithFormat:@"%C", oneChar];
}
[array addObject:result];
}
}
return [array componentsJoinedByString:@""];
}
+(NSString*)jongseongWithString:(NSString*)text
{
int firstCodeValue = (oneChar - 0xAC00)%28;
firstCodeValue += 0x11A7;
NSMutableArray<NSString*> * array = [NSMutableArray arrayWithCapacity:[text length]];
int i;
unichar oneChar, target;
NSString *result;
@autoreleasepool {
for (i=0; i<[text length]; i++) {
oneChar = [hangul characterAtIndex:i];
if (oneChar >= 0xAC00 && oneChar <= 0xD7A3) {
target = ((oneChar - 0xAC00)%28) += 0x11A7;
result = [NSString stringWithFormat:@"%C", target];
} else {
result = [NSString stringWithFormat:@"%C", oneChar];
}
[array addObject:result];
}
}
return [array componentsJoinedByString:@""];
}
@end
//
// main.m
// firstcode
//
// Created by sooop on 12. 1. 31..
// Copyright (c) 2012년 soooprmx.com All rights reserved.
//
#import <Foundation/Foundation.h>
#import "HangulSound.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSString *line, *result;
// insert code here...
// argc는 파라미터의 개수를 의미. 항상 1보다 크다. (첫 번째 파라미터는 파일 자신의 경로)
if (argc < 2) {
// 파라미터가 들어오지 않았음. 직접입력
printf(">> ");
NSFileHandle *stdin = [NSFileHandle fileHandleWithStandardInput];
NSData *data = [stdin availableData];
line = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
} else {
NSMutableArray<NSString*> * array = [NSMutableArray arrayWithCapacity:argc-1];
for (int i=1;i<argc;i++){
[array addObject:[NSString stringWithUTF8String:argv[i]];
}
line = [array componentsJoinedByString:@" "];
}
result = [HangulSound choseongWithString:line];
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