Добавить строку в mutablestring - PullRequest
       17

Добавить строку в mutablestring

0 голосов
/ 24 октября 2011
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";
consonants = "mra";// 2 согласных и 1 гласные

str = leo;
consonants = "leo";// 1 согласный и 2 гласных

Thk.

1 Ответ

1 голос
/ 24 октября 2011

Если вы знаете, что входная строка будет больше или равна 3, вы можете просто использовать цикл while:

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