fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. ios_base::sync_with_stdio(false);
  6. string s,be="",mid="",af="";
  7. cin>>s;
  8. int pos=s.length(),pos2=s.length();
  9. for(int i=0;i<s.length();i++)
  10. {
  11. if(s[i]!='a') // this thing looks for the first non 'a' character in the string.
  12. {pos=i;break;}
  13. else
  14. be=be+s[i];// until we find non 'a' character the string is stored in be.
  15. }
  16. //cout<<"before "<<be<<endl;
  17. if(pos+1<=s.length()) // if there is a non 'a' character present in the string
  18. {
  19. for(int i=pos;i<s.length();i++) // from the first non 'a' character in the string to length of string
  20. {
  21. if(s[i]=='a') // if i get an 'a' i record it's position in pos2 and breaks out of the loop
  22. {pos2=i;break;}
  23. else
  24. mid=mid+char(s[i]-1);// until i encounter 'a' there is a cyclic shift
  25. }
  26. //cout<<"mid "<<mid<<endl;
  27. if(pos2+1<=s.length()) // if we encounter an 'a' and there is some more string left
  28. {
  29. for(int i=pos2;i<s.length();i++) //we print it as it is
  30. af=af+s[i];
  31. }
  32. //cout<<"after "<<af<<endl;
  33. cout<<be<<mid<<af<<"\n"; // Hence be is before encountering first non 'a' character ,mid is cyclic shift of characters ,af is remaining string
  34. }
  35. else // if string only comprises of 'a'
  36. {
  37. for(int i=0;i<s.length()-1;i++)
  38. cout<<s[i];
  39. cout<<"z"<<"\n";
  40.  
  41. }
  42. }
Success #stdin #stdout 0s 2880KB
stdin
aabbbcc
stdout
aaaaabb