2011-10-24 120 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