Skip to content

Instantly share code, notes, and snippets.

@abdulateef
Created June 18, 2016 08:02
Show Gist options
  • Save abdulateef/28fe9681f92ada7c5012cd5402487f5d to your computer and use it in GitHub Desktop.
Save abdulateef/28fe9681f92ada7c5012cd5402487f5d to your computer and use it in GitHub Desktop.
Given a string,S , of length that is indexed from 0 to N-1 , print its even-indexed and odd-indexed characters as space-separated strings on a single line . Note:0 is considered to be an even index.
import java.util.Scanner;
public class Day6{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
input.nextLine();
String[] words = new String[n];
StringBuffer even = new StringBuffer();
StringBuffer odd = new StringBuffer();
for (int i=0; i<n; i++){
words[i] = input.nextLine();
for (int j=0; j<words[i].length(); j++){
if (j%2==0){
even.append(words[i].charAt(j));
}else {
odd.append(words[i].charAt(j));
}
}
System.out.println(even+" "+odd);
even.delete(0,even.length());
odd.delete(0, odd.length());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment