Given a string, print the string in the following pattern
Input string is 12345
Output is
Input string is 12345
Output is
1 5
2 4
3
2 4
1 5
Implementation:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { char s[50][50],v[50]; int N,i,j; /* Get the input string from the user */ scanf("%s",v); /* copy the string to 2-D nxn where n is the string length */ for(i=0;i<strlen(v);i++) strcpy(s[i],v); /* calculate the length of the string */ N=strlen(v); for(i=0;i<N;i++) { for(j=0;j<N;j++) { /* print the left diagonal */ if(i==j) printf("%c",*(*(s+i)+j)); /* print the right diagonal */ else if(j==N-i-1) printf("%c",s[i][N-i-1]); /* print space character in all other cases */ else printf(" "); } printf("\n"); } return 0; }