Reverse String according to the number of words | Java program to reverse string according to the number of words ; Reverse the letters of the word ; Temporary variable to store character ; Swapping the first and last character ; This function forms the required string ; Checking the number of words present in string to reverse ; Reverse the letter of the words ; Driver Code
class GFG { static void reverse ( char str [ ] , int start , int end ) { char temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } } static void reverseletter ( char str [ ] , int start , int end ) { int wstart , wend ; for ( wstart = wend = start ; wend < end ; wend ++ ) { if ( str [ wend ] == ' ▁ ' ) { continue ; } while ( wend <= end && str [ wend ] != ' ▁ ' ) { wend ++ ; } wend -- ; reverse ( str , wstart , wend ) ; } } public static void main ( String [ ] args ) { char str [ ] = " Ashish ▁ Yadav ▁ Abhishek ▁ Rajput ▁ Sunil ▁ Pundir " . toCharArray ( ) ; reverseletter ( str , 0 , str . length - 1 ) ; System . out . printf ( " % s " , String . valueOf ( str ) ) ; } }