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

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

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

想。

4

1 回答 1

1

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

int i = 0;
while([consonant length]<3){
   [consonant appendFormat:@"%c",[vocali characterAtIndex:i]];
   i++
}
于 2011-10-24T15:27:41.327 回答