Reverse the String Using C program
C Program:
Output 1:
Output 2:
From the above program,
ü Let us take the string as "WinG" which is stored in the variable str[100].
ü n=strlen(str), therefore n=4.
ü a[n] will be the temporary variable.
ü x=n/2 => x=4/2 => x=2 which is used for the iteration purpose.
ü for loop is explained in the below table.
i |
i<x |
a[i]=str[i] |
str[i]=str[n-i-1] |
str[n-i-1]=a[i] |
i++ |
0 |
0<2 |
a[0]=str[0] a[0]=W |
str[0]=str[4-0-1] str[0]=str[3] str[0]=G |
str[4-0-1]=a[0] str[3]=a[0] str[3]=W |
1 |
1 |
1<2 |
a[1]=str[1] a[1]=i |
str[1]=str[4-1-1] str[1]=str[2] str[1]=n |
str[4-1-1]=a[1] str[2]=a[1] str[2]=i |
2 |
2 |
2<2 false |
Doesn’t execute |
ü finally, the string will be printed in the reverse order.
0 Comments