To print the string pattern using c

To print the string pattern using c

Question:

To print the string pattern using a given odd length word which should be printed from the middle of the word.

Logic:

  • Get the input string from the user.
  • Find the middle position of the string.
  • Prints the second half of the string.
  • And then prints the first half by including the second half.

Algorithm:

  1. Calculate the length of the input string.
  2. And find the middle position of the string.
  3. Then print the spaces according to the length.
  4. Print the second half of the string orderly.
  5. Then print the first half by including the second half that gives the pattern like structure.

Program:

#include<stdio.h>
void main()
{
    char a[20];
    int i,j,n,c;
    clrscr();
    printf("enter the string:");
    gets(a);
    n=strlen(a);//stores the length of string
    c=n/2;//finds middle index of the string
    for(i=0;i<n;i++)
    {
	for(j=n-1;j>i;j--)//prints the spaces
	{
	   printf("  ");
        }
	for(j=c;j<=c+i&&j<n;j++)//prints the right half of the string
	{
	   printf("%c",a[j]);
	}
	if(i>c)//checks whether it prints all the right half elements
	{
           for(j=0;j<i-c;j++)//prints the left half of the string
	   {
	       printf("%c",a[j]);
	   }
        }
	printf("\n");
    }
    getch();
}

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

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.
0 0 votes
Article Rating
Subscribe
Notify of
guest
5.4K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
5.4K
0
Would love your thoughts, please comment.x
()
x