Skip to content

Commit 25cfa52

Browse files
authored
Add Baklava in Objective C (#4118)
1 parent bb0714f commit 25cfa52

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

archive/o/objective-c/baklava.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#import <Foundation/Foundation.h>
2+
3+
// Source:
4+
// https://stackoverflow.com/questions/260945/create-nsstring-by-repeating-another-string-a-given-number-of-times
5+
@interface NSString (Baklava)
6+
- (NSString *) repeatString: (NSUInteger) times;
7+
@end
8+
9+
@implementation NSString (Baklava)
10+
- (NSString *) repeatString: (NSUInteger) times {
11+
return [
12+
@""
13+
stringByPaddingToLength: [self length] * times
14+
withString:self startingAtIndex: 0
15+
];
16+
}
17+
@end
18+
19+
int main(int argc, const char * argv[]) {
20+
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
21+
22+
int i;
23+
for (i = -10; i <= 10; i++) {
24+
int numSpaces = abs(i);
25+
int numStars = 21 - 2 * numSpaces;
26+
printf(
27+
"%s%s\n",
28+
[[@" " repeatString: numSpaces] UTF8String],
29+
[[@"*" repeatString: numStars] UTF8String]
30+
);
31+
}
32+
33+
[pool drain];
34+
return 0;
35+
}

0 commit comments

Comments
 (0)