Skip to content

Commit 44b0ee3

Browse files
reverse word of a string
1 parent 3ef2232 commit 44b0ee3

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

reverse word.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# https://www.facebook.com/hitesh.parashar.908/posts/109890874217667
2+
# submitted by hitesh
3+
4+
5+
6+
public class Example
7+
{
8+
public void reverseWordInMyString(String str)
9+
{
10+
/* The split() method of String class splits
11+
* a string in several strings based on the
12+
* delimiter passed as an argument to it
13+
*/
14+
String[] words = str.split(" ");
15+
String reversedString = "";
16+
for (int i = 0; i < words.length; i++)
17+
{
18+
String word = words[i];
19+
String reverseWord = "";
20+
for (int j = word.length()-1; j >= 0; j--)
21+
{
22+
/* The charAt() function returns the character
23+
* at the given position in a string
24+
*/
25+
reverseWord = reverseWord + word.charAt(j);
26+
}
27+
reversedString = reversedString + reverseWord + " ";
28+
}
29+
System.out.println(str);
30+
System.out.println(reversedString);
31+
}
32+
public static void main(String[] args)
33+
{
34+
Example obj = new Example();
35+
obj.reverseWordInMyString("Welcome to BeginnersBook");
36+
obj.reverseWordInMyString("This is an easy Java Program");
37+
}
38+
}

0 commit comments

Comments
 (0)