samedi 25 février 2017

Odd length diagonal string pattern

I have written a program to print the given string diagonally (like x) what is wrong in my code ? please help

import java.util.*;

class Codechef {

public static void main (String[] args) throws java.lang.Exception
{

    Scanner in = new Scanner(System.in);
    String s = in.nextLine();
    char[] word = new char[100];
    word = s.toCharArray();
    int count=0;
    int k = s.length()-1;

    for(int i=0;i<s.length();i++){

        for(int j=0;j<s.length();j++){

           if(!(i==k))
            System.out.println(word[i]);

            for(int x=0;x<s.length()-count;x++)
                System.out.print(" ");



           System.out.println(word[k]);
           k--;
           count++;

           break; 
        }

        for(int j=0;j<count;j++)
            System.out.print(" ");    

    }

}

}

Aucun commentaire:

Enregistrer un commentaire