Finally my first batch of students, trained in iOS development for over the past six months are facing up with interview boards across the Middle East. In this series of tutes I have summarized a few common problems posed by technical recruiters to freshers aspiring to develop iOS applications.
Here’s one classic – write a function to spit out a sentence in the reverse order. Thus you take in
A quick brown fox jumped over the lazy dog
and output
dog lazy the over jumped fox brown quick A
This is how you do it in Obj-C. To demonstrate simple functions, I encourage students to use the Command Line Tool in Mac application as the project type, with Foundation selected in the drop down. Follow the screen shots below
Here’s the code
NSString *myString = @"A quick brown fox jumped over a lazy dog";
NSArray *myWords = [myString componentsSeparatedByString: @" "];
NSMutableString* theString = [NSMutableString string];
for (int i=[myWords count]-1; i>=0;i--){
[theString appendFormat:@"%@ ", [myWords objectAtIndex:i]];
}
NSLog(@"%@", theString);
The componentSeparatedByString method returns an Array with each word of the sentence as an object, in this case separated by a space. A string my string is initialized and constructed by the appendFormat method which loops through the index of the Array myWords decrementing from 7 to 0, obtained by using the method count.





Post your comment