File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package Strings ;
2+
3+ public class Strings {
4+
5+ public static void main (String [] args ) {
6+
7+ // learningCharacterArray("Pradeep");
8+ // reverseString("abcdefg");
9+ String output = reverseWordsInASentence ("My Name is Pradeep" );
10+ System .out .println (output );
11+ }
12+
13+ static String reverseWordsInASentence (String S ){
14+
15+ String [] array = S .split (" " );
16+
17+ int lengthOfString = array .length ;
18+
19+ String reversedString = "" ;
20+
21+ for (int i = lengthOfString -1 ; i >= 0 ; i --) {
22+ if (i == 0 ){
23+ reversedString = reversedString + array [i ];
24+ }else {
25+ reversedString = reversedString + array [i ] + " " ;
26+ }
27+ }
28+
29+ return reversedString ;
30+
31+ }
32+
33+ static void reverseString (String S ){
34+
35+ char [] characterArray = S .toCharArray ();
36+
37+ int p1 = 0 ; int p2 = characterArray .length -1 ;
38+
39+ while (p1 < p2 ){
40+ char t = characterArray [p1 ];
41+ characterArray [p1 ] = characterArray [p2 ];
42+ characterArray [p2 ] = t ;
43+ p1 = p1 + 1 ;
44+ p2 = p2 -1 ;
45+ }
46+
47+ for (int i = 0 ; i < characterArray .length ; i ++) {
48+ System .out .print (characterArray [i ]);
49+ }
50+ }
51+
52+ static void learningCharacterArray (String S ){
53+ //.toCharArray can convert your string to a char[]
54+
55+ char [] ch = S .toCharArray ();
56+ int n = ch .length ;
57+ for (int i = 0 ; i < n ; i ++) {
58+ System .out .print (ch [i ]);
59+ }
60+ }
61+ }
You can’t perform that action at this time.
0 commit comments