-
Notifications
You must be signed in to change notification settings - Fork 3
/
prog20.java
43 lines (35 loc) · 894 Bytes
/
prog20.java
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
//20. program to input a string from user and reverse each word of the string
package chapter3;
import java.util.Scanner;
public class prog20
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
String s; //input sstring
System.out.println("enter the string :");
s = sc.nextLine();
String rev = reverse(s);//function calling
System.out.println(rev);
}
//function to reverse the string
public static String reverse(String s)
{
String s1="",rev= "";
for (int i =0;i<s.length();i++)
{
s1 = s1+ s.charAt(i);
if((s.charAt(i)== ' ')||(i == s.length()))
{
for(int j = s1.length()-1;j>=0;j--)
{
rev = rev + s1.charAt(j);
if ((j==0) &&(i!= s.length()))
rev += " ";
}
s1 = "";
}
}
return rev;
}
}