Skip to content

Commit ed04615

Browse files
committed
Upated ReverseWordsInSentence Program
1 parent 7c3bd12 commit ed04615

2 files changed

Lines changed: 4 additions & 8 deletions

File tree

0 Bytes
Binary file not shown.

‎src/interviewQuestions/ReverseWordsInSentence.java‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
public class ReverseWordsInSentence {
77
public static void main(String[] args) {
88
ReverseWordsInSentence obj=new ReverseWordsInSentence();
9-
// obj.logic1();
10-
obj.logic2();
9+
obj.logic1();
10+
// obj.logic2();
1111
// obj.logic3();
1212
}
1313

@@ -16,13 +16,11 @@ public void logic1() {
1616
String s="";
1717
char[] ch=str.toCharArray();
1818
for(int i=0;i<ch.length;i++) {
19-
2019
int k=i;
21-
2220
while (i<ch.length && ch[i]!=' ') {
2321
i++;
2422
}
25-
for(int j=i-1;j>=k;j--) {
23+
for(int j=i-1;j>=k;j--) { // Reverse String logic
2624
s=s+str.charAt(j);
2725
}
2826
s=s+" ";
@@ -34,10 +32,8 @@ public void logic2() {
3432
String str="This is me"; // siht si em
3533
String s="";
3634
String[] words = str.split(" ");
37-
3835
for(int i = 0; i < words.length; i++) {
39-
for(int j = words[i].length()-1; j >= 0; j--) {
40-
36+
for(int j = words[i].length()-1; j >= 0; j--) { // Reverse String logic
4137
s += words[i].charAt(j);
4238
}
4339
s += " ";

0 commit comments

Comments
 (0)