Breaking

Saturday, 16 September 2017

Four Strings Square

                        Four Strings Square


Four strings all having the same length L are passed as the input to the program. The four strings must be printed in a L*L square matrix shape as shown in the example input/output.
The string which is on the top will always be the first string in the input. Other three strings can occur in a random order in the input. The sequence of the string can be identified by the fact that the last letter of a string will be the first letter of another string (and you can safely assume the last letter will not occur more than once).
Input Format:
The first line contains the string which represents the top of the square matrix.
The next three lines will contain the remaining the three string values which can represent the right, left and bottom side of the squares, but not necessarily in the same order.
Output Format:
The L*L square matrix with these four strings as it's sides as described in the Example Input/Output.
Boundary Conditions:
3 <=  L <= 100
Example Input/Output 1:
Input:
TIGER
YACHT
RANGE
EVERY
Output:
TIGER
H***A
C***N
A***G
YREVE
Example Input/Output 2:
Input:
MAN
DOT
NOD
TIM
Output:
MAN
I*O
TOD

Code:


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char a[4][100];int x2,x1;
    for(int i=0;i<4;i++)
    cin>>a[i];
    int l=strlen(a[0]);
    char b[l][l];
    for(int i=0;i<l;i++)
    b[0][i]=a[0][i];
    for(int i=1;i<4;i++){
        if(a[0][0]==a[i][l-1]){
            x1=i;break;
        }
    }
    for(int i=1;i<4;i++){
        if(a[0][l-1]==a[i][0]){
            x2=i;break;
        }
    }
    int x3=(6)-x2-x1;
    for(int i=0;i<l;i++){
        for(int j=0;j<l;j++){
            if(j==0)
            b[i][j]=a[x1][l-i-1];
            else if(j==l-1)
            b[i][j]=a[x2][i];
            else if(i==l-1)
            b[i][j]=a[x3][l-j-1];
            else if(i!=0)
            b[i][j]='*';cout<<b[i][j];
        }
        cout<<"\n";
    }
    return 0;
}

Logic 2:
#include <iostream>
 
using namespace std;

int main(int argc, char** argv)
{
string a[4];
int i,j,k,l;
for(i=0;i<4;i++)
    cin>>a[i];
l=a[0].length();
string e,t;
for(i=1;i<4;i++)
if(a[0][0]==a[i][l-1])
t=a[i];
for(i=1;i<4;i++)
if(t[0]==a[i][l-1])
e=a[i];
for(i=1;i<4;i++)
if(a[i]!=e && a[i]!=t)
break;
cout<<a[0]<<"\n";
for(j=0;j<l-2;j++) {
    cout<<t[l-2-j];
    for(k=0;k<l-2;k++)
    cout<<"*";
    cout<<a[i][j+1];
    cout<<"\n";
}
for(i=l-1;i>=0;i--)
cout<<e[i];

}
Please do comment If u have any Queries!

3 comments:

  1. import java.util.*;
    public class MyClass {

    public static void main(String[] args) {
    //Your Code Here
    Scanner sc=new Scanner(System.in);
    String s[]=new String[4];
    s[0]=sc.nextLine();
    s[1]=sc.nextLine();
    s[2]=sc.nextLine();
    s[3]=sc.nextLine();
    //System.out.println(s[0]);
    int len=s[0].length();
    char a[][]=new char[len][len];
    /*a[0][0]=s[0].charAt(0);
    a[0][1]=s[0].charAt(1);
    a[0][2]=s[0].charAt(2);
    a[0][3]=s[0].charAt(3);
    a[0][4]=s[0].charAt(4);*/
    for(int u=0;u=0;k--){
    System.out.println(arr[k]);
    }
    }
    }
    }*/
    if(a[0][0]==s[1].charAt(s[1].length()-1)){
    int b=s[1].length()-2;
    for(int i=1;i<len;i++){
    a[i][0]=s[1].charAt(b--);

    }
    }
    if(a[0][0]==s[2].charAt(s[2].length()-1)){
    int b=s[2].length()-2;
    for(int i=1;i<len;i++){
    a[i][0]=s[2].charAt(b--);

    }
    }
    if(a[0][0]==s[3].charAt(s[3].length()-1)){
    int b=s[3].length()-2;
    for(int i=1;i<len;i++){
    a[i][0]=s[3].charAt(b--);
    }
    }
    if(a[0][len-1]==s[1].charAt(0)){
    for(int i=1;i<s[1].length();i++){
    a[i][len-1]=s[1].charAt(i);
    }
    }
    if(a[0][len-1]==s[2].charAt(0)){
    for(int i=1;i<s[2].length();i++){
    a[i][len-1]=s[2].charAt(i);
    }
    }
    if(a[0][len-1]==s[3].charAt(0)){
    for(int i=1;i<s[3].length();i++){
    a[i][len-1]=s[3].charAt(i);
    }
    }
    if(a[len-1][0]==s[1].charAt(s[1].length()-1)){
    int j=s[1].length()-2;
    for(int i=1;i<len-1;i++){
    a[len-1][i]=s[1].charAt(j--);
    }
    }
    if(a[len-1][0]==s[2].charAt(s[1].length()-1)){
    int j=s[2].length()-2;
    for(int i=1;i<len-1;i++){
    a[len-1][i]=s[2].charAt(j--);
    }
    }
    if(a[len-1][0]==s[3].charAt(s[2].length()-1)){
    int j=s[3].length()-2;
    for(int i=1;i<len-1;i++){
    a[len-1][i]=s[3].charAt(j--);
    }
    }
    for(int k=1;k<len-1;k++){
    for(int n=1;n<len-1;n++){
    a[k][n]='*';
    }
    }
    for(int l=0;l<len;l++){
    for(int m=0;m<len;m++){
    System.out.print(a[l][m]);
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  2. import java.util.*;
    public class MyClass {

    public static void main(String[] args) {
    //Your Code Here
    Scanner sc=new Scanner(System.in);
    String s[]=new String[4];
    s[0]=sc.nextLine();
    s[1]=sc.nextLine();
    s[2]=sc.nextLine();
    s[3]=sc.nextLine();
    //System.out.println(s[0]);
    int len=s[0].length();
    char a[][]=new char[len][len];

    for(int u=0;u<len;u++){
    a[0][u]=s[0].charAt(u);
    }

    for(int z=1;z<s.length;z++){
    if(a[0][0]==s[z].charAt(s[z].length()-1)){
    int b=s[z].length()-2;
    for(int i=1;i<len;i++){
    a[i][0]=s[z].charAt(b--);

    }
    }
    if(a[0][len-1]==s[z].charAt(0)){
    for(int i=1;i<s[z].length();i++){
    a[i][len-1]=s[z].charAt(i);
    }
    }
    if(a[len-1][0]==s[z].charAt(s[z].length()-1)){
    int j=s[z].length()-2;
    for(int i=1;i<len-1;i++){
    a[len-1][i]=s[z].charAt(j--);
    }
    }
    }
    for(int k=1;k<len-1;k++){
    for(int n=1;n<len-1;n++){
    a[k][n]='*';
    }
    }
    for(int l=0;l<len;l++){
    for(int m=0;m<len;m++){
    System.out.print(a[l][m]);
    }
    System.out.println();
    }
    }
    }

    ReplyDelete
  3. import java.util.ArrayList;
    import java.util.Scanner;

    public class matrix {
    public static void main(String[] args)
    {
    Scanner scan=new Scanner(System.in);
    String a=scan.next();
    String b=scan.next();
    String c=scan.next();
    String d=scan.next();
    int l=a.length();
    char f=a.charAt(0);
    char l1=a.charAt(l-1);
    int i1=1;
    System.out.println(a);
    int i=l-2;
    for(int k=0;k=0)
    {
    System.out.print(b.charAt(i));
    i--;
    break;
    }

    }
    else if(f==c.charAt(l-1))
    {
    while(i>=0)
    {
    System.out.print(c.charAt(i));
    i--;
    break;
    }


    }
    else if(f==d.charAt(l-1))
    {
    while(i>=0)
    {
    System.out.print(d.charAt(i));
    i--;
    break;
    }
    }
    }
    else if(j>0&&j=0;s--)
    {
    System.out.print(c.charAt(s));
    }
    }
    if(b.charAt(l-1)==d.charAt(0))
    {
    for(int s=l-1;s>=0;s--)
    {
    System.out.print(d.charAt(s));
    }
    }
    }
    if(l1==c.charAt(0))
    {
    if(c.charAt(l-1)==b.charAt(0))
    {
    for(int s=l-1;s>=0;s--)
    {
    System.out.print(b.charAt(s));
    }
    }
    if(c.charAt(l-1)==d.charAt(0))
    {
    for(int s=l-1;s>=0;s--)
    {
    System.out.print(d.charAt(s));
    }
    }
    }
    if(l1==d.charAt(0))
    {
    if(d.charAt(l-1)==b.charAt(0))
    {
    for(int s=l-1;s>=0;s--)
    {
    System.out.print(b.charAt(s));
    }
    }
    if(d.charAt(l-1)==c.charAt(0))
    {
    for(int s=l-1;s>=0;s--)
    {
    System.out.print(c.charAt(s));
    }
    }
    }



    }

    }



    ReplyDelete

Like