i am retrieving different characters from a string by using thsi function and adding 5 to them to display the corresponding character for eg. 'a' displays 'f' and 'h' displays 'm'.. but the problem is that i am not able to add these characters into a string which i can use to display to display like 'fm'...can anyone help?? heres the code strResult(mutablestring) is getting null only.
str=@"John";
int a=[str length];
for(i=0;i<a;i++)
{
char ch=[str characterAtIndex:i];
ch=ch+5;
temp=[NSString stringWithFormat:@"%c",ch];
[strResult appendString:temp];
NSLog(@"%c",ch);
}
First of all, you need to make sure you allocate the string strResult
, like so:
NSMutableString *strResult = [NSMutableString string];
Second; you can, and indeed should, use -appendFormat:
for adding the characters to the string; the temporary extra string is pretty useless.
What you want then:
NSString *str = @"abcdef";
NSMutableString *strResult = [NSMutableString string];
for (NSUInteger i = 0; i < [str length]; i++) {
char ch = [str characterAtIndex:i] + 5;
NSLog(@"%c", ch);
[strResult appendFormat:@"%c", ch];
}
NSLog(@"%@", strResult);
This should produce:
f
g
h
i
j
k
fghijk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With