Hello @neharika.srivastav,
To understand the logic you are required to understand the syntax of the statements that it is executing in the program:
Let’s understand with an example:
-
suppose, n=3
-
string str(n, ’ ');
syntax:
string (size_t n, char c);
This would create a string of length n and would initialize all the characters of the string str with ’ ’ (space)
-
Now, let’s iterate the for loop:
for i=1:
str[n-i]=str[3-1]=str[2]="#"
i.e. the last character is set to “#”
So, now the string contain 2 spaces followed by a “#”
for i=2:
str[n-i]=str[3-2]=str[2]="#"
i.e. the second last character is set to “#”
So, now the string contain 1 spaces followed by 2 “#”
for i=3=n:
str[n-i]=str[3-3]=str[0]="#"
i.e. the first character is set to “#”
So, now the string contain 3 “#”
Output:
ss#
s##
###
(s=space)
Hope, this would help.
Give a like, if you are satisfied.