C Program For String Pattern

String Pattern in C | Write a C program that accepts a word from the user and prints it in the following way. Pre-requisite to write these programs are:- Different ways to read and display String in C, and Pattern Programs in C.

Half Pyramid String Pattern in C

Program-1:- Write C program for the below String Pattern.

Enter a string: COMPUTER
C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
COMPUTER

The code to display the above pattern is given below, 

#include<stdio.h>
int main()
{
  // variable
  char str[20];

  // take input
  printf("Enter a string: ");
  scanf("%[^\n]",str);

  // print pattern
  // outer loop for row
  for(int i=0; str[i]!='\0'; i++)
  {
    // inner loop for column
    for(int j=0; j<=i; j++)
    {
      // display
      printf("%c", str[j]); 
    }

    printf("\n"); // new line
  }

  return 0;
}

Output:-

Enter a string: PROGRAM
P
PR
PRO
PROG
PROGR
PROGRA
PROGRAM

In this program, first, we declared a variable to store the array of characters. The str[20] can store a maximum of 20 characters. The scanf(“%[^\n]”, str); line reads input until (if there is enough space) the user press the enter key. Learn more:- Different ways to read and display String in C.

The for(int i=0; str[i]!=’\0′; i++) will iterate until the end of the String. This pattern contains N characters in the Nth line i.e. in the 1st line it contains 1 character, the 2nd line contains 2 characters, and so on. We can assume it as a matrix representation where lines will represent by rows and characters will be represented by columns. Therefore 1st row contains 1 column, the 2nd row contains 2 columns, and so on.

If you are finding difficulties in understanding inner and outer loop concepts then first you should practice this:- Pattern Programs in C

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

While using the inner and outer loop, the outer loop represents the number of rows and the inner loop represents columns. The total number of rows in this pattern is equal to the total number of characters in the string, therefore the outer row should be iterate until the end of the String. Every valid string ends with the ‘\0’ character.

The inner loop (column) is dependent upon the row number. In 1st row, there should be only 1 column and only 1 character should be displayed. Therefore iterate the inner loop until the current row value. After execution of each inner loop print in the new line.


Half Diamond String Pattern

Program-2:- Write C program for the below String Pattern.

Enter a string: HELLO
H
HE
HEL
HELL
HELLO
HELL
HEL
HE
H

The code for the above pattern is,

#include<stdio.h>
#include<stdlib.h>
int main()
{
   // variables
   char str[20];
   int len, place;

   // take input
   printf("Enter a string: ");
   scanf("%[^\n]", str);

   // find length of string
   for(len=0; str[len]!='\0'; len++);
   // actual length is length - 1
   len--;
 
   // outer loop for row
   for(int i=0; i<(2*len+1); i++)
   {
     // find the place
     if(i<len) place = i;
     else place = abs(2*len - i);

     // inner loop for column
     for(int j=0; j<=place; j++)
     printf("%c",str[j]); // print

     printf("\n"); // next line
   }

   return 0;
}

Output:-

Enter a string: KNOW
K
KN
KNO
KNOW
KNO
KN
K

The “len” variable stores the length of the string and the “place” variable stores the value of how many characters should be printed in a row.


Program-3:- Write C program for the below half diamond string pattern in C.

Enter a string: KNOW
K
KN
KNO
KNOW
NOW
OW
W

The C program code for the above half diamond string program is given below.

#include<stdio.h>
int main()
{
  // variable
  char str[20];
  int len;

  // take input
  printf("Enter a string: ");
  scanf("%[^\n]",str);

  // count number of characters
  for (len = 0; str[len]!='\0'; len++);

  // for first half
  for(int i=0; i<len; i++)
  {
    for(int j=0; j<=i; j++)
    {
      printf("%c", str[j]); 
    }

    printf("\n"); // new line
  }

  // for second half
  for(int i=1; i<len; i++)
  {
    for(int j=i; j<len; j++)
    {
      printf("%c", str[j]); 
    }

    printf("\n"); // new line
  }
  
  return 0;
}

Output:-

Enter a string: PROGRAM
P
PR
PRO
PROG
PROGR
PROGRA
PROGRAM
ROGRAM
OGRAM
GRAM
RAM
AM
M


Mirrored Half Diamond String Pattern

Program-4:- Write C program for the below mirrored half diamond string pattern.

Enter a string: PROGRAM
      P
     PR
    PRO
   PROG
  PROGR
 PROGRA
PROGRAM
 ROGRAM
  OGRAM
   GRAM
    RAM
     AM
      M

The code for the above pattern is given below,

#include<stdio.h>
int main()
{
  // variable
  char str[20];
  int len;

  // take input
  printf("Enter a string: ");
  scanf("%[^\n]",str);

  // count number of characters
  for (len = 0; str[len]!='\0'; len++);

  // for first half
  for(int i=0; i<len; i++)
  {
    for(int j=0; j<len-i-1; j++)
    {
      printf(" "); // space
    }
    for(int j=0; j<=i; j++)
    {
      printf("%c", str[j]); // character
    }

    printf("\n"); // new line
  }

  // for second half
  for(int i=1; i<len; i++)
  {
    for(int j=0; j<i; j++)
    {
      printf(" "); // space
    }
    for(int j=i; j<len; j++)
    {
      printf("%c", str[j]); 
    }

    printf("\n"); // new line
  }
  
  return 0;
}

Pyramid String Pattern

Program-5:- Write C program for the below pyramid string pattern.

Enter a string: PROGRAM
Enter number of rows: 5
      P
     R O
    G R A
   M P R O
  G R A M P
 R O G R A M

The code for the above pattern is,

#include<stdio.h>
int main()
{
   // variables
   char str[20];
   int n, a=0;

   // take input values
   printf("Enter a string: ");
   scanf("%[^\n]", str);
   printf("Enter number of rows: ");
   scanf("%d", &n);

   // outer loop for row
   for(int i=0;i<=n;i++)
   {
     // for space 
     for(int j=0;j<=n-i;j++) 
     printf(" "); // print space

     for(int k=0;k<=i;k++)
     {
        // print character
        printf("%2c", str[a++]);

        // if index reach end of string then again
        // it should start from initial characters
        if(str[a]=='\0') a=0;
     }

     printf("\n"); // new line
   }

   return 0;
}

Output:-

Enter a string: KNOWPROGRAM
Enter number of rows: 9
         K
        N O
       W P R
      O G R A
     M K N O W
    P R O G R A
   M K N O W P R
  O G R A M K N O
 W P R O G R A M K
N O W P R O G R A M

In this program number of rows and strings is taken from the user. The number of columns in each is equal to the row number. So first we give space where space is (total row – current row). We have taken one separate variable “a” to keep track of the index of string. In the pattern when the string reached the end, then displaying again starts from the start of the string. For example in the word “PROGRAM”, when the pattern reached character “M” then again it starts from character “A”.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

12 thoughts on “C Program For String Pattern”

  1. Enter a word: PHYSICS
    Output:

          P
          H
          Y
    P H Y S I C S
          I
          C
          S
    

    Can I have the java program for the above problem using nested loops and not with an array. Please.

    1. The solution for the above program is added as program-3 (Half Diamond String Pattern). And for the Mirrored Half Diamond String Pattern, the program-4 is added.

    1. Here is the code for the above pattern.

      // variables
      char str[20];
      int len;
      
      // take input
      printf("Enter a string: ");
      scanf("%[^\n]", str);
      
      // find length of string
      for(len=0; str[len]!='\0'; len++);
      // actual length is length - 1
      len--;
      
      // print pattern 
      // Outer loop for row
      for(int i=len; i>=0; i--)
      {
        // inner loop for column
        for(int j=0; j<=i; j++)
        printf("%c", str[j]);
      
        printf("\n"); //next line
      }
      
    1. #include<stdio.h>
      int main()
      {
            char str[20];
            int i,j,k;
      
            printf("Enter a string: ");
            scanf("%[^n]",str);
      
            for(i=0;str[i]!='';i++)
            {
              for(j=0; j <= i; j++)
              printf("%c ", str[j]);
              
              for(k=i-1; k >= 0; k--)
              printf("%c ", str[k]);
              
              printf("\n");
            }
      
            return 0;
      }
      

      Output:-


      Enter a string: SURAT
      S
      S U S
      S U R U S
      S U R A R U S
      S U R A T A R U S

Leave a Comment

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