Pattern 412 (String Pattern)

Pattern 412 (String Pattern) post thumbnail image

C

#include <stdio.h>
#include <string.h>

int main()
{
  //A char array to hold the string and print a single char at a time.

  char arr[]="SUMIT";

  int len=strlen(arr);
  int i,j;

  for (i=0; i<len; i++)
  {
    for (j=0; j<=i; j++)
    {
      printf("%c ",arr[j]);
    }
    printf("\n");
  }

  return 0;
}

C++

#include<iostream.h>
#include<string.h>

int main()
{
//A char array to hold the string and print a single char at a time.

    char arr[]="SUMIT";

    int len=strlen(arr);
    int i,j;

    for (i=0; i<len; i++)
    {
        for (j=0; j<=i; j++)
        {
            cout<<arr[j]<<" ";
        }
        cout<<endl;
    }

    return 0;
}

Java

class PatternProg
{
    public static void main(String args[])
    {
        String str = "SUMIT";
        for (int i = 0; i <str.length(); i++)
        {
            for (int j = 0; j <= i; j++)
            {
                System.out.print(str.charAt(j));
            }
            System.out.println();
        }
    }
}

C#

using System;

class PatternProg
{
  public static void Main()
  {
    string str = "SUMIT";
    for (int i = 0; i < str.Length; i++)
    {
      for (int j = 0; j <= i; j++)
      {
        Console.Write(str[j]);
      }
      Console.WriteLine();
    }
    Console.ReadKey(true);
  }
}

Python

string = "SUMIT"
strlen = len(string)

for x in range(0, strlen):
  print(string[0:x+1])
5 1 vote
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns