-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQue46_ReverseStringNotWords.java
More file actions
53 lines (47 loc) · 1.09 KB
/
Copy pathQue46_ReverseStringNotWords.java
File metadata and controls
53 lines (47 loc) · 1.09 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
51
52
53
/*
46. How to reverse words in a sentence without using a library method?
Write a function, which takes a String word and returns sentence on which words are reversed in
order like if the input is "Java is best programming language", the output should be "language
programming best is Java".
*/
import java.util.*;
public class Que46_ReverseStringNotWords
{
public static void main(String x[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String ");
String s=sc.nextLine();
String str=new String();
for(int i=0; i<s.length(); i++)
{
str=str+s.charAt(s.length()-1-i);
}
System.out.println(str);
int start=0;
int end=0;
char ch[]=str.toCharArray();
for(int i=0; i<str.length(); i++)
{
if(i==str.length()-1 || ch[i]==' ')
{
end=i-1;
if(i==str.length()-1)
{
end=i;
}
while(start<end)
{
char temp=ch[start];
ch[start]=ch[end];
ch[end]=temp;
start++;
end--;
}
start=i+1;
}
}
String ss=new String (ch);
System.out.println(ss);
}
}