将字符串附加到可变字符串
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 = leo;
辅音=“leo”; // 1 个辅音和 2 个元音
Thk.
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]];
}
My problem is the follow:
if there are less of 3 consonant, I must append n vowels to consonants string.
Examples:
str = "mario";
consonants = "mra"; // 2 consonants and 1 vowels
str = leo;
consonants = "leo"; // 1 consonant and 2 vowels
Thk.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您知道输入字符串将大于或等于 3,则可以使用 while 循环:
If you know that the input string will be greater or equal to 3 you can just use a while loop: