简单的Objective C程序 - 它不可能是这么复杂

时间:2014-01-16 18:06:13

标签: objective-c

我正在学习Objective C并决定创建一个简单的命令行程序。这个想法是要求你提供你的名字,然后向后显示它,将每个单词的第一个字母大写。我完成了它,但解决方案似乎过于复杂。我不禁觉得有更好的方法。

            char word [256];
    printf("What is your name: ");
    scanf("%[^\n]s",word);

    // Convert the char array to NSString
    NSString * inputString = [[NSString alloc] initWithCString: word encoding: NSUTF8StringEncoding];

    //This will be our output string
    //NSString *nameReversed = [[NSString alloc] init];  //alloc, init are needed to create an instance of this object
    NSString *nameReversed = @"";

    // Make inputString all lower case
    inputString = [inputString lowercaseString];

    // Get length of inputString and type cast it as an int and decrement by one
    int length = (int)([inputString length])-1;

    BOOL foundSpace = NO;

    for (int i = 0; i<=(length); i++) {
        // Setup the range
        NSRange range = NSMakeRange(length-i,1);

        // Get the a char from the input string
        NSString *inputChar = [inputString substringWithRange:range];

        // If this is the first char then make it upper case
        if (i==0) {
            inputChar = [inputChar capitalizedString];
        }

        // See if the last char was a space and if so make this char upper case
        if (foundSpace){
            foundSpace = NO;  // Reset foundSpace
            // Set this char to upper case
            inputChar = [inputChar capitalizedString];
        }

        // See if this char is a space.  If so, we'll need to convert the next char to upper case
        if ([inputChar  isEqual: @" "]) {
            foundSpace = YES;
        }

        // Add the char to nameReversed
        nameReversed = [nameReversed stringByAppendingString:inputChar];

    }

    printf("%s \n", [nameReversed UTF8String]);

任何见解都将受到赞赏!

2 个答案:

答案 0 :(得分:2)

您的程序无法正确处理组合字符序列。

此外,capitalizedString将大写字符串中每个单词的第一个字母。所以你可以打电话一次。

static NSString *reversedString(NSString *string) {
    NSMutableString *reversed = [NSMutableString stringWithCapacity:string.length];
    [string enumerateSubstringsInRange:NSMakeRange(0, string.length)
        options:NSStringEnumerationReverse
            | NSStringEnumerationByComposedCharacterSequences
        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        [reversed appendString:substring];
    }];
    return reversed;
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSData *inputData = [[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile];
        NSString *original = [[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding];
        NSString *reversed = reversedString(original);
        NSString *reversedAndCapitalized = [reversed capitalizedString];
        printf("%s\n", reversedAndCapitalized.UTF8String);
    }
    return 0;
}

在真实应用中,我可能会在NSString上添加一个定义reversedString方法的类别,而不是将其设为免费功能。

答案 1 :(得分:0)

如果您假设输入采用ascii编码。

char word [256];
printf("What is your name: ");
scanf("%[^\n]s", word);

// reverse
for (NSInteger i=0,j=strlen(word)-1; i<j; i++,j--) {
    char tmp = word[i];
    word[i] = word[j];
    word[j] = tmp;
}

NSString *str0 = [NSString stringWithCString:word encoding:NSASCIIStringEncoding];
NSLog(@"%@", [str0 capitalizedString]);