Slip 21 - A) Write a java program to display each word from a file in reverse order.

Solution :

import java.io.*;
import java.util.Scanner;

class Slip21A{
    public static void main(String args[]) throws IOException{
        FileReader fr = new FileReader("a.txt");
        FileWriter fw = new FileWriter("b.txt");

        try (Scanner dr = new Scanner(fr)) {
            while(dr.hasNextLine()){
                String s=dr.nextLine();
                StringBuffer buffer = new StringBuffer(s);
                buffer=buffer.reverse();
                String ans = buffer.toString();
                fw.write(ans);
            }
        }catch(Exception e){
            System.out.print("Error...!");
        }

        fr.close();
        fw.close();
    }
}

Output:


a.txt

Output_pic

b.txt

Output_pic



Post a Comment

0 Comments