[Interview programming questions] Algorithm basics-character shift

Source: Internet
Author: User
Tags uppercase letter

Error code

#include <iostream>#include<string>using namespacestd;BOOLIslow (Charc) {    if((c>='A') && (c<='Z'))        return false; Else         return true;}BOOLIsCap (Charc) {    return!Islow (c); }intMain () {strings;  while(cin>>s) {intn=s.size (); intL=0, r=n-1;  while(l<r) { while(r>=0&& IsCap (S[r])) r--;//a lowercase letter at the far right of R             while(l<=n-1&& Islow (S[l])) l++;//l point to the left-most uppercase letter            if(l<r) Swap (S[l],s[r]); Else                  Break; } cout<<s<<Endl; }}

Test Cases

Test Case: Hxklagllzpytxsfsrunnskqbhdqqroyaeyjrgijbhidxfcqkfmihpnkibfhxxdbdkavgziblvwnlxjahmttssjkzhsmqnenvokoiyzrjpqsrffaaqzbynyerj Vkvfrcgdfycidtqbyqcpatdrgzzbaakoqybwmoyhrcqdwcrwqqpqavtnabjrivwxjortyjvgyswzkyenagqbzkjlucabnyvyvfxagklfqhxnttaqzfncedtro Gmzzndbvzbbarbjvuyivlwrkaagrvtyxrscuoqxdwcrmveedrlkzkfjvrmrlsmbmogujyfdzirfhuswjqgrtymlxkqnmacavatlqirzmfqvywgqtvenxucpkq Cauqbjyfanuwondtbnldgrtpucqodqsujoddpuczjwczaasddeyjkvitumhrcmzqslrjiefvisatiutfxbeknhpyvwukzrlifsywgexogiegxdbfxaybwfuqf Yevjfihepdpkqeigutzhddidbgkpvbfyqheeehatoabqhepidihigbtwjghiqrctzxqqykffowubqzyicjrpqbilhrnqnbzfmoruycsrgkawjccormceegisp Ipsgvjbngwvmtytgoalqfpfyofaxndjznfkdtwfixisktjyjchidxpyglfobolriuiahmabqwohbgbduybhldqgzjasshszoepthlvnybnqwegmonexfdstvy The hgtdmlyugefobsqmgndboxkkhvhfvryoovoyxdjqjljysngksbtopopjfskqzheplukxyytycew corresponding output should be: Hxzyxssrnndryagibckmhfxdvgiwnlxmttskhmneoojqsraaqbyyejrdfycidqbycptdzzaoqybyhrdwcwpavnbjriwxrzeqzkucabvyxfqttaqfncdroznbv AbvuvlraarvtyxrsqxdwrmermrsmbmyfdrhuwxaavatlmvygxcabjyfauwodldgrtCodqsudpczaasdkviturmljefisattfxenyvzlisgeogigbfxybwuqyvjfqithdpvyqehobqpdhtjictxkfobqycjilrnqzmorkawcrceegppjbngtolyxndf Wxisjyjchidpgforiumbwogbdlsszthnbqegmexfdsgtmlyugefsqmgoxkkhfvrooyxjngksboposzeukyeklagllptfuskqbhqqoeyjrjhidxfqfipnkibhx Dbkazblvjahsjzsqnvkiyzrpffznrvkvfcgtqargbakwmocqrqqqtavjotyjvgyswkynagbjlnyvfagklhxnzetgmzdzbbrjyiwkgcuocvedlkzkfjvrloguj Zifsjqgrtymlkqnmcqirzfqwqtvenupkqcuqnntbnpuqjodujwczdeyjmhczqsriviubkhpwukrfywxexdaffeihepdpkeguzdidbgkbfheeataheiiigbwgh Qrzqqyfwuzirpqbhnbfruycsgjcomisisgvwvmtygaqfpfofajznkdtfiktxylboliahaqhbuybhdqgzjashoeplvynwontvyhdobndbvhyvodjqjlystpjfk QHPLXYTYCW your output is: Hxeykuezzsyoxspsronnbskgndjxryyaoorvgifbhkkxocgkmmqhsfegufyxlmtdgsvgdifxewnlxmgemttsqbknhhmtnezsosoldbgjoqsrwbaaqmbyuyeij Rofgrpddfycidiqbyhcpctdjyzzjsaioqybxwfyhrdndwcxwylpoavtngbjrinwxbjrppgeecrczwaekroqmzkzqucabnrvylixjcyqfqbofttaqkfncxdtro Cizjntbvhdpaqbbvuohvlerqaayrvtyxrsvpdqxdwhrmtieqrfjvyquwbmrysmbmxfbgyfdigrohuewgsilzvynxexftatavatlasifmejvylgmrutixvckds AaazbjyfacuwopddUsldgrtqdcoqupnbjotdnunqjwczucqdkeyjpunevmhtcqzqswrqifzvriqciumnqbkklhpmywuktrrgfqywjsxfizejxdugoalrvfjffkezklihepdpkdeeg Uvzcodidbgkucbfgkhweeiatyajrhebibizigbdwzghmqrgtzeqqyznfxwuhlzkigarpqbfvhynlnbjfbgruycsagnykjcwosmygvjisyitsgvojvawvmtytg Qaqqfpfqofarqcjznokdtmfiwkaktbgraqtgxcyflvkbolvrniahzafqfphbrzyuybhidqgzjaskhvnoepqslvzyjnswhajonvlbzatvyhkbddxhbiknobpif Qndbfxdihvhjrjyyevooqdjqjlqyshbqkstuftpjfpkqlhlplgaxlytyckw

[Interview programming questions] Algorithm basics-character shift

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.