We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<iostream>#include<string>usingnamespacestd;intmain(){//declare both stringsstrings1,s2;//read both strings from inputcin>>s1>>s2;//write size of both stringscout<<s1.size()<<' '<<s2.size()<<endl;//write both strings combinedcout<<s1+s2<<endl;//swap first chars of both strings using XOR//no need to use swap method or tmp variableif(s1[0]!=s2[0]){s1[0]^=s2[0];s2[0]^=s1[0];s1[0]^=s2[0];}//write both stringscout<<s1<<' '<<s2;return0;}
I encourage You to memorize and use XOR swap method as it is probably the easiest and most efficient way for such cases.
Also this, beside string/array reverse, is the most popular interview question for C/C++ developer position.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Strings
You are viewing a single comment's thread. Return to all comments →
This is my solution:
I encourage You to memorize and use XOR swap method as it is probably the easiest and most efficient way for such cases. Also this, beside string/array reverse, is the most popular interview question for C/C++ developer position.