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 = "マリオ";
子音 = "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 に答える