Digital 7-Shaped String

Given an input String S, accept the input string S and print the string as shown in the Example Input/ Outputsection.

Input Format:
The first line contains the value of the string S. S may contain alphabets, numbers and special characters.

Output Format:
The strings are printed in the Digital 7-Shaped format.

Boundary Conditions:
1 < Length of S <1000

Example Input/ Output 1:
Input:

xyz

Output:
x x x
y y x
z y x

Example Input/ Output 2:
Input:

123456

Output:
1 1 1 1 1 1
2 2 2 2 2 1
3 3 3 3 2 1
4 4 4 3 2 1
5 5 4 3 2 1
6 5 4 3 2 1

#include <stdio.h>   // Standard input/output library
#include <stdlib.h>  // Standard library for functions like strlen

int main() {
    int i, j, k, l;  // Variables used for loop counters and length of the input string
    char a[1000];    // Character array to store the input string

    // Read a string from the user and store it in the character array 'a'
    scanf("%s", a);

    // Calculate the length of the input string
    l = strlen(a);

    // Outer loop to print each line
    for (i = 0; i < l; i++) {
        k = i;  // Initialize 'k' to the current value of 'i'

        // Inner loop to print characters in each line
        for (j = 0; j < l; j++) {
            // Check if the current column is before the halfway point of the line
            if (j < (l - i))
                printf("%c ", a[i]);  // Print the 'i'-th character from the input string
            else {
                printf("%c ", a[k - 1]);  // Print the previous character from the input string
                k--;  // Decrement 'k' to move backward in the input string
            }
        }

        printf("n");  // Print a new line after each row of characters is printed
    }

    return 0;  // Indicate successful program execution
}
def main():
    a = input()  # Input a string from the user
    l = len(a)   # Calculate the length of the input string

    # Outer loop to print each line
    for i in range(l):
        k = i  # Initialize 'k' to the current value of 'i'

        # Inner loop to print characters in each line
        for j in range(l):
            # Check if the current column is before the halfway point of the line
            if j < (l - i):
                print(a[i], end=" ")  # Print the 'i'-th character from the input string
            else:
                print(a[k - 1], end=" ")  # Print the previous character from the input string
                k -= 1  # Decrement 'k' to move backward in the input string

        print()  # Print a new line after each row of characters is printed

if __name__ == "__main__":
    main()
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String a = scanner.nextLine();  // Input a string from the user
        int l = a.length();  // Calculate the length of the input string

        // Outer loop to print each line
        for (int i = 0; i < l; i++) {
            int k = i;  // Initialize 'k' to the current value of 'i'

            // Inner loop to print characters in each line
            for (int j = 0; j < l; j++) {
                // Check if the current column is before the halfway point of the line
                if (j < (l - i)) {
                    System.out.print(a.charAt(i) + " ");  // Print the 'i'-th character from the input string
                } else {
                    System.out.print(a.charAt(k - 1) + " ");  // Print the previous character from the input string
                    k--;  // Decrement 'k' to move backward in the input string
                }
            }

            System.out.println();  // Print a new line after each row of characters is printed
        }
    }
}
using System;

public class Program {
    public static void Main() {
        string a = Console.ReadLine();  // Input a string from the user
        int l = a.Length;  // Calculate the length of the input string

        // Outer loop to print each line
        for (int i = 0; i < l; i++) {
            int k = i;  // Initialize 'k' to the current value of 'i'

            // Inner loop to print characters in each line
            for (int j = 0; j < l; j++) {
                // Check if the current column is before the halfway point of the line
                if (j < (l - i)) {
                    Console.Write(a[i] + " ");  // Print the 'i'-th character from the input string
                } else {
                    Console.Write(a[k - 1] + " ");  // Print the previous character from the input string
                    k--;  // Decrement 'k' to move backward in the input string
                }
            }

            Console.WriteLine();  // Print a new line after each row of characters is printed
        }
    }
}
function main() {
    const input = require('readline-sync');
    const a = input.question('Enter a string: ');  // Input a string from the user
    const l = a.length;  // Calculate the length of the input string

    // Outer loop to print each line
    for (let i = 0; i < l; i++) {
        let k = i;  // Initialize 'k' to the current value of 'i'
        let line = '';

        // Inner loop to construct each line
        for (let j = 0; j < l; j++) {
            // Check if the current column is before the halfway point of the line
            if (j < (l - i)) {
                line += a[i] + ' ';  // Add the 'i'-th character from the input string to the line
            } else {
                line += a[k - 1] + ' ';  // Add the previous character from the input string to the line
                k--;  // Decrement 'k' to move backward in the input string
            }
        }

        console.log(line);  // Print the constructed line
    }
}

main();
#include <iostream>
#include <string>

int main() {
    std::string a;
    std::cin >> a;  // Input a string from the user
    int l = a.length();  // Calculate the length of the input string

    // Outer loop to print each line
    for (int i = 0; i < l; i++) {
        int k = i;  // Initialize 'k' to the current value of 'i'

        // Inner loop to print characters in each line
        for (int j = 0; j < l; j++) {
            // Check if the current column is before the halfway point of the line
            if (j < (l - i)) {
                std::cout << a[i] << ' ';  // Print the 'i'-th character from the input string
            } else {
                std::cout << a[k - 1] << ' ';  // Print the previous character from the input string
                k--;  // Decrement 'k' to move backward in the input string
            }
        }

        std::cout << std::endl;  // Print a new line after each row of characters is printed
    }

    return 0;
}
Previous Article

Unique String Representations

Next Article

Distinct Length Pairs

Write a Comment

Leave a Comment

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