将字符串附加到mutablestring

时间:2011-10-24 15:16:57

标签: iphone objective-c nsmutablestring

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个辅音,我必须将n个元音附加到辅音串。

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

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

THK。

1 个答案:

答案 0 :(得分:1)

如果您知道输入字符串将大于或等于3,则可以使用while循环:

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