Reverse Each word in a String

Program to Reverse each word in String

package InterviewPrograms;

import java.util.Scanner;

public class ReverseEachWord {

// Method 01
public static String revWordsOfString(String str) {
// s1.Split the String with whitespace


This content originally appeared on DEV Community and was authored by Suresh Ayyanna

Program to Reverse each word in String

package InterviewPrograms;

import java.util.Scanner;

public class ReverseEachWord {

    // Method 01
    public static String revWordsOfString(String str) {
        // s1.Split the String with whitespace
        String[] words = str.split(" ");

        // s2. Using for each loop read each word and reverse it
        String revstr = "";
        for (String w : words) {
            String revword = "";
            for (int i = w.length() - 1; i >= 0; i--) {
                revword = revword + w.charAt(i);
            }
            revstr = revstr + revword + " ";
        }
        return revstr;
    }

//Method 02
    public static String revWordOfString(String str) {
        // s1.Split the String with Space Reg.Expression(\\s)

        String[] word = str.split(" \\s");

        String revword = "";
        for (String w : word) {
            StringBuilder sb = new StringBuilder(w);
            sb.reverse();

            revword = revword + sb.toString() + " ";
        }
        return revword;
    }

    public static void main(String[] args) {
        System.out.println("Enter the Actual String:");
        Scanner input = new Scanner(System.in);
        String actualString = input.nextLine();

        String ExpectedString = revWordsOfString(actualString);

        System.out.println("The Reverse word of each string of given String from Method 1: " + ExpectedString);
        System.out.println(
                "The Reverse word of each string of given String from Method 2:" + revWordOfString(actualString));
    }
}


This content originally appeared on DEV Community and was authored by Suresh Ayyanna


Print Share Comment Cite Upload Translate Updates
APA

Suresh Ayyanna | Sciencx (2022-01-15T15:36:55+00:00) Reverse Each word in a String. Retrieved from https://www.scien.cx/2022/01/15/reverse-each-word-in-a-string/

MLA
" » Reverse Each word in a String." Suresh Ayyanna | Sciencx - Saturday January 15, 2022, https://www.scien.cx/2022/01/15/reverse-each-word-in-a-string/
HARVARD
Suresh Ayyanna | Sciencx Saturday January 15, 2022 » Reverse Each word in a String., viewed ,<https://www.scien.cx/2022/01/15/reverse-each-word-in-a-string/>
VANCOUVER
Suresh Ayyanna | Sciencx - » Reverse Each word in a String. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/15/reverse-each-word-in-a-string/
CHICAGO
" » Reverse Each word in a String." Suresh Ayyanna | Sciencx - Accessed . https://www.scien.cx/2022/01/15/reverse-each-word-in-a-string/
IEEE
" » Reverse Each word in a String." Suresh Ayyanna | Sciencx [Online]. Available: https://www.scien.cx/2022/01/15/reverse-each-word-in-a-string/. [Accessed: ]
rf:citation
» Reverse Each word in a String | Suresh Ayyanna | Sciencx | https://www.scien.cx/2022/01/15/reverse-each-word-in-a-string/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.