Print square pattern for the given string
This program can print the given string in the square pattern in the square like structure.
Logic:
- Get the input string from the user.
- Check its length to print square pattern.
- Then print the top row and left column.
- Print bottom row and right column in the square pattern.
Algorithm:
- Get the input string from the user.
- Find the length of the given string.
- By using the length and its index values print the square.
- By printing top, left, bottom, right orderly.
- At last, it gives the square like structure.
Program:
#include<stdio.h> #include<conio.h> void main() { char a[100]; int i,j,n,k; clrscr(); printf("Enter the string"); gets(a); n=strlen(a); for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==0)// prints the top row { printf("%c ",a[j]); } else if(i!=n-1) { if(j==0)//prints the left column { printf("%c ",a[j+i]); } else if(j==n-1) { printf("%c ",a[j-i]);// prints the right column } else { printf(" "); } } else//prints the bottom row { printf("%c ",a[n-j-1]); } } printf("\n"); } getch(); }</pre>