Programming Examples

Java to swap first character of each word with next character


java to swap first character of each word with next character

Solution

import java.util.*;
class SwapFirstChar
{ 
    public static void main(String[] args) 
    { 
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any String");
        String str = sc.nextLine(); 
        char[] ch = str.toCharArray(); 
        char temp;
        temp=ch[0];
        ch[0]=ch[1];
        ch[1]=temp;
        for (int i = 0; i < ch.length; i++) 
        { 
            if(ch[i]==' ')
            {
                temp=ch[i+1];
                ch[i+1]=ch[i+2];
                ch[i+2]=temp;
            }
        }
        System.out.println(String.valueOf(ch)); 
    } 
} 
Output


Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad