Skip to content

Commit 3ef2232

Browse files
added reverse word for every string
1 parent 4ab84e5 commit 3ef2232

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

reverseword.java

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

0 commit comments

Comments
 (0)