Program to insert dashes between two adjacent odd digits in given Number | C ++ program for 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
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; bool checkOdd ( char ch ) { return ( ( ch - '0' ) & 1 ) ; } string Insert_dash ( string num_str ) { string result_str = num_str ; for ( int x = 0 ; x < num_str . length ( ) - 1 ; x ++ ) { if ( checkOdd ( num_str [ x ] ) && checkOdd ( num_str [ x + 1 ] ) ) { result_str . insert ( x + 1 , " - " ) ; num_str = result_str ; x ++ ; } } return result_str ; } int main ( ) { string str = "1745389" ; cout << Insert_dash ( str ) ; return 0 ; }