Read a string, interchange adjacent characters. If the length of the string is odd then the last character need not to be chang | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Read a string, interchange adjacent characters. If the length of the string is odd then the last character need not to be chang

Can anyone please tell me how to do this I am not getting any output... please do it is urgent... https://code.sololearn.com/cboezjCTyIaN/?ref=app

7th Apr 2022, 7:41 AM
Nandini Bhardwaj
Nandini Bhardwaj - avatar
12 Answers
+ 2
//See this modified code: import java.util.*; class Q2 { public static void main(String[] arg) { int x; String s1=""; //empty string Scanner br=new Scanner(System.in); String s=br.next(); int l=s.length(); //for odd length, neglect last char if(l%2==1) l = l-1; for(x=0; x<l; x+=2) { s1=s1+ s.charAt(x+1)+ s.charAt(x); } if(s.length()%2==1) //get last char System.out.println(s1+s.charAt(l)); else System.out.println(s1); } } //hope it helps..
7th Apr 2022, 12:07 PM
Jayakrishna 🇮🇳
+ 1
Hi umm can you please frame this question for me as I am not able to get it....😅 can you please do this for me....
7th Apr 2022, 8:22 AM
Nandini Bhardwaj
Nandini Bhardwaj - avatar
+ 1
Jayakrishna🇮🇳 do tell me I have made some changes in my program now its saying not found please see and tell what to do next please 🥺🥺🥺🙏
7th Apr 2022, 11:37 AM
Nandini Bhardwaj
Nandini Bhardwaj - avatar
+ 1
Jayakrishna🇮🇳 I know I am disturbing you but please check once what's the issue now please please 🥺🥺🙏....I have done everything u said now please 🥺
7th Apr 2022, 12:22 PM
Nandini Bhardwaj
Nandini Bhardwaj - avatar
+ 1
Jayakrishna🇮🇳 thanks a lot sir.... It is all correct now
7th Apr 2022, 12:50 PM
Nandini Bhardwaj
Nandini Bhardwaj - avatar
+ 1
Jayakrishna🇮🇳 thanks I owe you a lot
7th Apr 2022, 1:01 PM
Nandini Bhardwaj
Nandini Bhardwaj - avatar
0
I want note mistakes few : You have mismatching braces.. if(l%2==0) { // you are missing this brace { for(x=0;x<l;x=x+2) { char ch=s.charAt(x); char ch1=s.charAt(x+1); s1=ch+ch1; // character addition works as ascii value additions so you get integer result back 'a' + 'b' results 97+98=195 . If you want as "ab" then do like 'a' + "" + 'b' . So ch1 + ""+ ch works fine. Note you need it reverse for your problem.. } System.out.println(s1); } If you want to print total string at last then use s = s + ch1 + ch ; Before this intialuze s as = s = ""; empty string.. Hope it helps to correct your program... Remove s brace at last an extra one also... for second loop, you need condition x<l-1; after you can correct your program as needed output...
7th Apr 2022, 8:20 AM
Jayakrishna 🇮🇳
0
Firstly you try to understand and reply specific about where you not understanding.. I try to correct if any mistakes.. 🥱Am more lazy than you...
7th Apr 2022, 8:27 AM
Jayakrishna 🇮🇳
0
for(x=0;x<l;x++) // x = x++; this post increment don't update x. It's last it's increment by overriding x to x. Just use x++. { //char ch=s.charAt(x); //char ch1=s.charAt(x+2); s1=s1+s.charAt(x+2)+s.charAt(x); // missing object s.charAt() You can also use s1 = s1 + ch1+ch; if you compute ch1, ch otherwise remove those.. } Do you want to swap adjacents Or odd even positions?
7th Apr 2022, 11:48 AM
Jayakrishna 🇮🇳
0
Input is Dear So output should be eDra Like this..
7th Apr 2022, 11:52 AM
Nandini Bhardwaj
Nandini Bhardwaj - avatar
0
I added comments in my corrected code. Read again for changes I made. You have extra space by adding String s1 = " "; instead just use s1=""; //empty string.. And now you can remove lines from 10 to 19.. It's extra now, as all done by next loop.. Post updates in new post, rather than changing original question.. It easy to understand then, for reader also. If any issue still for you then describe that so I can understand it easily.. Because about Other issues I already mentioned details.. Go through again all. If anything not clear, then you can reply with details...
7th Apr 2022, 12:42 PM
Jayakrishna 🇮🇳
0
Nandini Bhardwaj You're welcome.. Edit: char a=s.charAt(x); char b=s.charAt(x+1); s1 = s1 + b + a; Or s1=s1+s.charAt(x+1)+s.charAt(x); This both snippets works same. Use either one. You are not using a, b so you can remove..
7th Apr 2022, 1:00 PM
Jayakrishna 🇮🇳