-4

I have an array "names" which contains names starting with letters a, b and c.(like anju,chandu,basha,chitra,amith,baskar)

I have three NSMutablearray as,bs and cs.

Now the question is how to get the names from "names" array assign the names starting with letter 'a' to 'as' mutablearray, names starting with letter 'b' to 'bs' mutablearray and names starting with letter 'c' to 'cs' mutablearray?

Could anyone please help..

Thank you.

5
  • Post your current progresses, please.
    – giampaolo
    Commented Sep 10, 2013 at 5:51
  • only a,b & c or? and post some-code here...
    – KarSho
    Commented Sep 10, 2013 at 5:53
  • So you're telling me you don't know how to enumerate an array, test the first character of each string element and then make a decision about which array the string should be added to? What do you know?
    – trojanfoe
    Commented Sep 10, 2013 at 5:54
  • wow..looks like home assignment given by the lecturer... Commented Sep 10, 2013 at 5:58
  • Hint: Use NSPredicate on the array like: SELF beginswith[c] 'a', etc.
    – Amar
    Commented Sep 10, 2013 at 7:34

3 Answers 3

0

I think this helps you

NSArray *namesArray = @[@"anju",@"chandu",@"basha",@"chitra",@"amith",@"baskar"];
NSMutableArray *aArray = [[NSMutableArray alloc]init];
NSMutableArray *bArray = [[NSMutableArray alloc]init];
NSMutableArray *cArray = [[NSMutableArray alloc]init];
for (int j=0; jNSString *string = [namesArray objectAtIndex:j]; if ([string hasPrefix:@"a"]) { [aArray addObject:string]; }else if([string hasPrefix:@"b"]){ [bArray addObject:string]; }else if([string hasPrefix:@"c"]){ [cArray addObject:string]; } } NSLog(@"---%@----%@----%@",aArray,bArray,cArray);
0

Hope this will Help you.

for (NSString *thisString in names) {
    if ([thisString characterAtIndex:0] == 'a') {
        [as addObject:thisString];
    }
    else if ([thisString characterAtIndex:0] == 'b') {
        [bs addObject:thisString];
    }
    else if ([thisString characterAtIndex:0] == 'c') {
        [cs addObject:thisString];
    }
}
0

Try this..

for(int i =;i<names.length;i++){
   NSString *myString =names[i];
   NSString *subString = [myString substringWithRange: NSMakeRange(0, 1)];
   if([subString isEqualToString:@"a"){
     [as addObect:subString];
   }else if([subString isEqualToString:@"b"){
     [bs addObect:subString];
   }else if([subString isEqualToString:@"c"){
     [cs addObect:subString];
   }
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.