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
using System ; class GFG { public static string nextWord ( string str ) { if ( str == " " ) { return " a " ; } int i = str . Length - 1 ; while ( str [ i ] == ' z ' && i >= 0 ) { i -- ; } if ( i == - 1 ) { str = str + ' a ' ; } else { str = str . Substring ( 0 , i ) + ( char ) ( ( int ) ( str [ i ] ) + 1 ) + str . Substring ( i + 1 ) ; } return str ; } public static void Main ( string [ ] args ) { string str = " samez " ; Console . Write ( nextWord ( str ) ) ; } }