forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseString.java
More file actions
50 lines (44 loc) · 1.08 KB
/
ReverseString.java
File metadata and controls
50 lines (44 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Main
{
public static void main(String[] args) {
String s="I love my India";
int i=s.length()-1;
String ans="";
while(i>=0)
{
if(i<0)
break;
while(i>=0 && s.charAt(i)==' ')
i--;
int j=i;
while(i>=0 && s.charAt(i)!=' ')
i--;
if(ans.isEmpty()){
ans=ans.concat(s.substring(i+1,j+1));
}else{
ans=ans.concat(" "+s.substring(i+1,j+1));
}
}
System.out.println(ans);
}
}
// Java program to ReverseString using ByteArray.
import java.lang.*;
import java.io.*;
import java.util.*;
// Class of ReverseString
class ReverseString {
public static void main(String[] args)
{
String input = "GeeksforGeeks";
// getBytes() method to convert string
// into bytes[].
byte[] strAsByteArray = input.getBytes();
byte[] result = new byte[strAsByteArray.length];
// Store result in reverse order into the
// result byte[]
for (int i = 0; i < strAsByteArray.length; i++)
result[i] = strAsByteArray[strAsByteArray.length - i - 1];
System.out.println(new String(result));
}
}