2011-10-24 122 views
0
NSString *str= @"surname"; 
NSMutableString *consonants = [[NSMutableString alloc] init]; 
NSMutableString *vowels = [[NSMutableString alloc] init]; 

for (int i=0; i < [str length]; i++){ 
    if ([str characterAtIndex:i] != 'a' && [str characterAtIndex:i] != 'e' && [str characterAtIndex:i] != 'i' && [str characterAtIndex:i] != 'o' && [str characterAtIndex:i] != 'u') { 
      [consonants appendFormat:@"%c",[str characterAtIndex:i]]; 
     } 
     else{ 
      [vowels appendFormat:@"%c",[str characterAtIndex:i]]; 
     } 
    } 


if([consonants length] < 3){ 
     [consonants appendFormat:@"%@", [vocali characterAtIndex:1]]; 
} 

我的问题是后续:
如果有较少的3辅音,我必须ñ元音附加到辅音串。追加字符串mutablestring

示例:
str =“mario”;
辅音=“mra”; // 2个辅音和1个元音

str = leo;
辅音=“leo”; // 1个辅音和2个元音

Thk。

+0

我已经阅读并阅读你的问题,我只是不明白你想要 – Oliver

回答

1

如果您知道输入的字符串会大于或等于3,你可以只使用一个while循环:

int i = 0; 
while([consonant length]<3){ 
    [consonant appendFormat:@"%c",[vocali characterAtIndex:i]]; 
    i++ 
} 
+0

完美,thk太多了! – Mario

+1

如果回答问题,请选中左侧的复选标记。 – utahwithak