Program to insert dashes between two adjacent odd digits in given Number | Java program to implement the above approach ; Function to check if char ch is odd or not ; Function to insert dash - between any 2 consecutive digit in string str ; Traverse the string character by character ; Compare every consecutive character with the odd value ; Print the resultant string ; Driver Code ; Given number in form of string ; Function call
class GFG { static boolean checkOdd ( char ch ) { return ( ( ch - '0' ) & 1 ) != 0 ? true : false ; } static String Insert_dash ( String num_str ) { StringBuilder result_str = new StringBuilder ( num_str ) ; for ( int x = 0 ; x < num_str . length ( ) - 1 ; x ++ ) { if ( checkOdd ( num_str . charAt ( x ) ) && checkOdd ( num_str . charAt ( x + 1 ) ) ) { result_str . insert ( x + 1 , " - " ) ; num_str = result_str . toString ( ) ; x ++ ; } } return result_str . toString ( ) ; } public static void main ( String [ ] args ) { String str = "1745389" ; System . out . println ( Insert_dash ( str ) ) ; } }