Given a string, of length that is indexed from to , print its even-indexed and odd-indexed characters as space-separated strings on a single line

Given a string,  of length  that is indexed from  to , print its even-indexed and odd-indexed characters as  space-separated strings on a single line 

I/P:

2
Hacker
Rank

O/P:

Hce akr
Rn ak



SOLUTION:


package Strings;
import java.util.*;
class oddevepositioncharacter{
    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        for(int j=0;j<t;j++)
        {
            String s1=sc.next();
            for(int i=0;i<s1.length();i++)
            {
                if(i%2==0)
                {
                    System.out.print(s1.charAt(i));
                }
            }
            System.out.print(" ");
              for(int i=0;i<s1.length();i++)
            {
                if(i%2!=0)
                {
                    System.out.print(s1.charAt(i));
                }
            }
            System.out.println();
            
            
        }
    }
}

Comments

Popular posts from this blog

PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION

Program to Copy One Array to Another using Assignment operator

PROGRAM TO FIND THE COUNT OF PARTICULAR NUMBER IN AN ARRAY