Skip to content

Commit 668bbec

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#96 from Rahul3330/patch-1
Create Swap the first word and last word
2 parents a54a4bd + 1aa583e commit 668bbec

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Swap the first word and last word

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#https://www.facebook.com/profile.php?id=100013277143928&__tn__=%2CdC-R-R&eid=ARBKbLxMZblgtFE4DpGns8ObnqM8AwcGnliVKTU_RNGlr2ePyy5v4Pn3iYvCKxZa0XLaGMlr3qDi5TlC&hc_ref=ARQ24E17s7oDHvteqiO7eOM3KzHs079jvEC9GCUvn4TCq5SdVIPDZK9jkcBpFkwXxfY&fref=nf
2+
3+
import java.util.Scanner;
4+
class MainStr12
5+
{
6+
public static void main(String[] args)
7+
{
8+
Scanner sc= new Scanner(System.in);
9+
System.out.println("Enter the Sentence.. ");
10+
String st=sc.nextLine();
11+
char ch[]=st.toCharArray();// string charcter array
12+
int f=0;
13+
int l=0;
14+
for(int i=0;i<ch.length;i++)
15+
{
16+
17+
if(i==0&&ch[i]!=' '||ch[i]!=' '&&ch[i-1]==' ')
18+
{
19+
f=i;
20+
}
21+
22+
if(i==ch.length-1&&ch[i]!=' '||ch[i]!=' '&&ch[i+1]==' ')
23+
{
24+
l=i;
25+
26+
char temp=ch[f];
27+
ch[f]=ch[l];
28+
ch[l]=temp;
29+
}
30+
}
31+
32+
st=new String(ch);//charcter array to String
33+
System.out.println(st);
34+
35+
}
36+
}

0 commit comments

Comments
 (0)