Pattern 423 (String Pattern)

Pattern 423 (String Pattern) post thumbnail image

C

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

int main()
{
  int c,d;
  char str[]="NtoS"; //put any string
  int stlen=strlen(str);

  for(c=0; c<stlen; c++)
  {
    d=c+1;
    printf("%.*s\n",d,str);
  }

  for(c=stlen-2; c>=0; c--)
  {
    d=c+1;
    printf("%.*s\n",d,str);
  }
  return 0;
}

C++

 #include<iostream.h>
 #include<string.h>
 
 int main()
 {
 char * str="NtoS";
 int len =strlen(str);
 
 for(int i=1;i<len;i++)
 {
 cout.write(str,i);
 cout<<endl;
 }
 
 for(int i=len;i>0;i--)
 {
 cout.write(str,i);
 cout<<endl;
 }
 }

Java

 class PatternProg
 {
 public static void main(String a[])
     {
 int c,d;
 String str="NtoS"; //put any string
 
 int stlen=str.length();
 
 for(c=0;c<stlen;c++)
 {
 d=c+1;
 System.out.println(str.substring(0, d));
 }
 
 for(c=stlen-2;c>=0;c--)
 {
 d=c+1;
 System.out.println(str.substring(0, d));
 }
 }
 }

C#

using System;
class PatternProg
{
  public static void Main(string[] a)
  {
    int c, d;
    string str = "NtoS"; //put any string

    int stlen = str.Length;


    Console.Write("\n\n");

    for (c = 0; c < stlen; c++)
    {
      d = c + 1;
      Console.WriteLine(str.Substring(0, d));
    }

    for (c = stlen - 2; c >= 0; c--)
    {
      d = c + 1;
      Console.WriteLine(str.Substring(0, d));
    }
    Console.ReadKey(true);
  }
}

Python

string = "NtoS"
strlen = len(string)
for x in range(0, strlen-1):
    print(string[0:x + 1])
for y in range(0, strlen):
    print(string[0:strlen - y])
0 0 votes
Rate this Program
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Related Patterns