Lexicographically next string | C ++ program to find lexicographically next string ; If string is empty . ; Find first character from right which is not z . ; If all characters are ' z ' , append an ' a ' at the end . ; If there are some non - z characters ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string nextWord ( string s ) { if ( s == " " ) return " a " ; int i = s . length ( ) - 1 ; while ( s [ i ] == ' z ' && i >= 0 ) i -- ; if ( i == -1 ) s = s + ' a ' ; else s [ i ] ++ ; return s ; } int main ( ) { string str = " samez " ; cout << nextWord ( str ) ; return 0 ; }