VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-insert-dashes-between-two-adjacent-odd-digits-in-given-number/

⇱ Program to insert dashes between two adjacent odd digits in given Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to insert dashes between two adjacent odd digits in given Number

Last Updated : 15 Jul, 2025

Given a large number in form of string N, the task is to insert a dash between two adjacent odd digits in the given number in form of strings.

Examples:

Input: N = 1745389 
Output: 1-745-389 
Explanation: 
In string str, str[0] and str[1] both are the odd numbers in consecutive, so insert a dash between them.
Input: N = 34657323128437 
Output: 3465-7-323-12843-7

Bitwise Approach:

  1. Traverse the whole string of numbers character by character.
  2. Compare every consecutive character using logical Bitwise OR and AND operators.
  3. If two consecutive characters of the string are odd, insert a dash (-) in them and check for the next two consecutive characters.

Below is the implementation of the above approach:


Output: 
1-745-389

 

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

Regular Expression Approach:

The given problem can be solved using Regular Expression. The RE for this problem will be: 
 

(?<=[13579])(?=[13579])
The given RE matches between odd numbers. We can replace the matched part of zero width with a dash, i.e.
str = str.replaceAll("(?<=[13579])(?=[13579])", "-");

Below is the implementation of the above approach:


Output: 
1-745-389

 

Time Complexity: O(N) 
Auxiliary Space: O(1)

Comment