0% found this document useful (0 votes)
23 views6 pages

String Pattern Generation in Java

The document contains 6 Java programs that print strings in different patterns. The programs take a string as input and use for loops to print the characters in the string in descending or ascending order on successive lines. This allows the string to be displayed as a triangular pattern. The programs demonstrate different approaches to print the characters of a string in triangular format using basic string and looping operations in Java.

Uploaded by

Sandipan Bhuina
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

String Pattern Generation in Java

The document contains 6 Java programs that print strings in different patterns. The programs take a string as input and use for loops to print the characters in the string in descending or ascending order on successive lines. This allows the string to be displayed as a triangular pattern. The programs demonstrate different approaches to print the characters of a string in triangular format using basic string and looping operations in Java.

Uploaded by

Sandipan Bhuina
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMPUTER APPLICATION

PROGRAM ON STRING PATTERN


PROGRAM:

/*

B
BL
BLU
BLUE
BLUEJ
*/

import [Link].*;
class p2 {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter String :");
String str = [Link]();
int l=[Link]();
char ch;
for(int i=0;i<l;i++) {
for(int j=0;j<=i;j++) {
ch=[Link](j);
[Link](ch);
}
[Link]();
}
2

/*
BLUEJ
BLUE
BLU
BL
B
*/
import [Link].*;
class a31 {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a Word.");
String str = [Link]();
int l=[Link]();
for(int i=l-1;i>=0;i--) {
for(int j=0;j<=i;j++) {
[Link]([Link](j));
}
[Link]();
}
}
}
3

/*
B
LL
UUU
EEEE
JJJJJ

*/
import [Link].*;
class p1 {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter String :");
String str = [Link]();
int l=[Link]();
char ch;
for(int i=0;i<l;i++) {
for(int j=0;j<=i;j++) {
ch=[Link](i);
[Link](ch);
}
[Link]();
}
}
4

/*
BLUEJ
LUEJ
UEJ
EJ
J
*/
import [Link].*;
class V31c {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Word:");
String str = [Link]();
int l=[Link]();
for(int i=0;i<l;i++) {
for(int j=i;j<l;j++) {
[Link]([Link](j));
}
[Link]();
}
}
}
5

/*
J
EE
UUU
LLLL
BBBBB
*/
import [Link].*;
class b31 {
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter String :");
String str = [Link]();
int l=[Link]();
for(int i=l-1;i>=0;i--) {
for(int j=l-1;j>=i;j--) {
[Link]([Link](i));
}
[Link]();
}
}
}
6
/*
B
LB
ULB
EULB
JEULB
*/

import [Link].*;
public class StringPatt
{
public static void StringPat()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a String = ");
String n = [Link]();
int l = [Link]();
for(int i=0;i<l;i++)
{
for(int j=i;j>=0;j--)
{
char ch = [Link](j);
[Link](ch+" ");
}
[Link]();

Common questions

Powered by AI

In the 'p2' class, the string pattern starts with the first character and gradually includes more characters from the start, forming a triangle that progressively grows larger with each line. In contrast, the 'a31' class starts with the entire word and removes the last character on each subsequent line, creating a triangle that decreases in size as it progresses.

The 'a31' class transforms a word into successively smaller substrings by truncating from the end, forming a decremental triangle pattern. The 'V31c' class transforms by starting from the first character moving rightward, truncating from the start, creating a pattern that decrements the starting character while preserving the end of the string intact.

The 'V31c' class generates a pattern by successively decreasing the starting index for reading characters from the input string, while retaining all subsequent characters to the end of the string. This results in a pyramid pattern where each line starts further along the string than the previous line.

The nested loops in each class determine both readability and execution order of operations. In 'p2' and 'a31', loops grow or shrink the range of characters respectively, contributing to straightforward execution flow for increasing or decreasing triangles. 'p1' and 'b31' use loops to manage repetitive index access, which can obscure loop purpose without careful analysis. 'StringPatt' employs loops to reverse iterate, introducing complexities in tracing execution flow due to non-linear backward growth patterns.

The 'b31' class repeats the character as many times as the reverse index allows, essentially reversing the growth process seen in 'p1', which repeats characters based on forward iteration indices. Unlike 'V31c', which focuses on substring creation with intact forward progression, 'b31' emphasizes character consolidation using backward progression.

In 'p1', the character index dictates the number of repetitions per line, increasing linearly. In 'b31', it indicates a backward index, dictating the number of output repetitions by decrementing from the end, forming an inverted triangle. In 'StringPatt', it controls sequential backward concatenation from the current character, influencing the staggered growth pattern.

A critical advantage of 'V31c's pattern is its illustrative demonstration of substring manipulation and iterative logic, beneficial for teaching index-based iteration concepts. Conversely, the disadvantage is the potential for confusion due to non-intuitive decrementally starting outputs, which may require additional explanation for novice programmers to grasp effectively.

In 'StringPatt', the string pattern is created by iterating over each character from the start of the string to the current character index in reverse, concatenating these characters with spaces in between. Each line starts with the current character, and the pattern grows downward, emphasizing the movement from the last added character backward, resulting in staggered repetition of characters.

The 'p1' class repeats each character of the string as many times as its current index position, resulting in a growing number of repeated characters in a forward iteration. Conversely, the 'b31' class starts iterating from the last character and moves backward, repeating each character as many times as its current backward index position, producing an inverted triangular pattern.

Both the 'p2' and 'StringPatt' classes use constant space complexity for processing, as they do not require additional space that scales with input size beyond variables used for control flow. However, they differ in their utilization; 'p2' constructs its output linearly from left to right, while 'StringPatt' constructs lines using a nested loop that accesses characters in reverse, slightly adjusting their operational complexity.

You might also like