我有两个字符串,如“SANFRANSICO”和“CHICAGO”
现在我想让它像字符串一样反向但是在单个字符串中,如“OOCGIASCNIAHRCFNAS”
我已经完成了代码,但它给了我错误的结果
查看我的代码
- (NSString *)changeString1:(NSString *)string string2:(NSString *)string2{
int first = [string length];
int second = [string2 length];
int total = first+second;
NSMutableString *str = [[NSMutableString alloc] init];
string = [self reverseString:string];
string2 = [self reverseString:string2];
for (int i=0; i<total+1; i++)
{
if (i%2==1) {
int j = i/2;
if(j < second){
NSString *ichar = [NSString stringWithFormat:@"%c", [string2 characterAtIndex:j]];
[str appendString:ichar];
}
else{
NSString *ichar = [NSString stringWithFormat:@"%c", [string characterAtIndex:j+1]];
NSLog(@"222 %d %@",j+1, ichar );
[str appendString:ichar];
check = YES;
}
}
else {
int j = i/2;
if(check == YES){
}
else{
NSString *ichar = [NSString stringWithFormat:@"%c", [string characterAtIndex:j]];
NSLog(@"1111 %d %@",j, ichar );
[str appendString:ichar];
}
}
}
return str;
}
答案 0 :(得分:0)
我发布此问题后很快就找到了答案,对不起发布此问题
查看我的答案
- (NSString *)changeString1:(NSString *)string string2:(NSMutableString *)string2{
int first = [string length];
int second = [string2 length];
string = [self reverseString:string];
string2 = [self reverseString:string2];
for(int i =second; i<first; i ++){
[string2 appendString:@" "];
}
int total = first*2;
NSMutableString *str = [[NSMutableString alloc] init];
for (int i=0; i<total; i++)
{
if (i%2==1) {
int j = i/2;
if(j < second){
NSString *ichar = [NSString stringWithFormat:@"%c", [string2 characterAtIndex:j]];
[str appendString:ichar];
}
}
else {
int j = i/2;
if(j < first){
NSString *ichar = [NSString stringWithFormat:@"%c", [string characterAtIndex:j]];
[str appendString:ichar];
}
else{
NSString *ichar = [NSString stringWithFormat:@"%c", [string2 characterAtIndex:j]];
[str appendString:ichar];
}
}
}
NSLog(@"str %@", str);
return str;
}