0% found this document useful (0 votes)
2 views1 page

Reverse String

The document contains a Java program that reverses each word in a given string. It defines a main method that splits the input string into words, reverses each word using a helper method, and then prints the reversed string. There are some syntax errors in the code that need to be corrected for it to compile successfully.

Uploaded by

guptasriya991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Reverse String

The document contains a Java program that reverses each word in a given string. It defines a main method that splits the input string into words, reverses each word using a helper method, and then prints the reversed string. There are some syntax errors in the code that need to be corrected for it to compile successfully.

Uploaded by

guptasriya991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

String name="this is my home";

Output -shit si ym emoh d

public class main(){


public static void main(String[] str){

String str = "this is my home"


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

StringBuilder reversedString = new StrinBuilder();


for(String word: words){
StringBuilder reversedWord = reverseWord(word);
reversedString.append(reversedWord).append(" ");
}

System.out.println("Reversed string:"+reversedString.toString().trim());

public static String reverseWord(String word){

char[] arr = word.toCharArray();


int left = 0;
int right = arr.length-1;

while(left<right){

char temp = arr[left];


char[left] = char[right];
char[right] = temp;
left++;
right--;

}
return new String(arr);

}
}

You might also like