-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion1859.java
More file actions
45 lines (41 loc) · 883 Bytes
/
Copy pathQuestion1859.java
File metadata and controls
45 lines (41 loc) · 883 Bytes
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
/*
Author: Ananthanarayanan R
Section: Algorthms
Question: 1859
*/
public class Question1859
{
public static String sortSentence(String s)
{
int count =0 ;
int start = 0;
for(int i =0;i<s.length();i++)
if(s.charAt(i)==' ')
count++;
String[] word = new String[count+1];
for(int i = 0;i<s.length();i++)
{
if(s.charAt(i) == ' ')
{
word[(int)(s.charAt(i-1)-49)] = s.substring(start,i-1);
start = i+1;
}
}
word[(int)s.charAt(s.length()-1)-49] = s.substring(start,s.length()-1);
s= "";
for(int i = 0;i<word.length;i++)
{
s = s+word[i];
if(i!=word.length-1)
s+=' ';
}
return s;
}
public static void main(String[] args)
{
System.out.println("Main Method starts");
String s = "is2 sentence4 This1 a3";
String result = sortSentence(s);
System.out.println(result);
}
}