If the length of resultant string is less than N than add a character ' a '
for ( int i = total ; i < N ; i ++ ) { ans += ' a ' ; } String reverse = " " ; int Len = ans . length ( ) - 1 ; while ( Len >= 0 ) { reverse = reverse + ans . charAt ( Len ) ; Len -- ; }