0

I need a program to print string like as follow below in the objective c:

PARTHIBAN
 ARTHIBA
  RTHIB
   THI
    H

I have tried with (*)

Code:

int i,j,k,height;
NSString *name;
NSLog(@"Enter the name: %@",name);
NSLog(@"Enter the height:");
scanf("%i",&height);
for (i=height;i>=1;i--)
{
    for(k=height-1;k>=i;k--)
    {
        NSLog(@" ");
    }
    for (j=i; j>=1; j--)
    {
        NSLog(@"*");
    }
}

Can you please help me print the string as mentioned above in Objective-C?

WangYudong
  • 4,107
  • 3
  • 27
  • 51
Parthiban
  • 150
  • 1
  • 13
  • Possible duplicate of [How do I check if a string contains another string in Objective-C?](http://stackoverflow.com/questions/2753956/how-do-i-check-if-a-string-contains-another-string-in-objective-c) – Parthiban Mar 28 '17 at 12:32

1 Answers1

2
NSMutableString *name = [NSMutableString stringWithString:@"PARTHIBAN"];
while (name.length > 1) {
    [name deleteCharactersInRange:NSMakeRange(0, 1)];
    [name deleteCharactersInRange:NSMakeRange(name.length - 1, 1)];
    NSLog(@"name: %@", name);
}

EDIT: I noticed you want to print a string with format, try this:

NSMutableString *name = [NSMutableString stringWithString:@"PARTHIBAN"];
for (int i = 0; i < name.length / 2; i++ ) {
    [name replaceCharactersInRange:NSMakeRange(i, 1) withString:@" "];
    [name replaceCharactersInRange:NSMakeRange(name.length - 1 - i, 1) withString:@" "];
    NSLog(@"name: %@", name);
}
WangYudong
  • 4,107
  • 3
  • 27
  • 51