Print square pattern for the given string

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:

  1. Get the input string from the user.
  2. Find the length of the given string.
  3. By using the length and its index values print the square.
  4. By printing top, left, bottom, right orderly.
  5. At last, it gives the square like structure.

Program:

[code lang=”c”]
#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>
[/code]

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published. Required fields are marked *