/给一个英文句子,把每个单词的首字母转成大写。 x 方法一 int main(int argc, const char * argv[]) { @autoreleasepool {
NSMutableString* str = [NSMutableString stringWithString:@"my name is osama bin mohammed bin away bin laden”];
int a = 0;
//遍历str中的字符 for (int i =0; i<str.length; i++) {
//如果不是空格,并且a是0,将字母大写。(只执行空格后的一个字符) if ([str characterAtIndex:i] != ' '&& a == 0) { NSString* change = [NSString stringWithFormat:@"%c",[str characterAtIndex:i]-32];
[str replaceCharactersInRange:NSMakeRange(i, 1) withString:change];
a++;
//如果是空格,将a赋值0 }else if([str characterAtIndex:i] == ' ') { a = 0; } } NSLog(@"%@",str);
} return 0; }
方法二 NSString* str = @"osama bin mohammed bin awad bin laden";
NSString* result = [str capitalizedString];
NSLog(@"%@",result);
|