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:
- Traverse the whole string of numbers character by character.
- Compare every consecutive character using logical Bitwise OR and AND operators.
- 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:
C++
// C++ program for the above approach
#include <iostream>
#include <string>
using namespace std;
// Function to check if char ch is
// odd or not
bool checkOdd(char ch)
{
return ((ch - '0') & 1);
}
// Function to insert dash - between
// any 2 consecutive digit in string str
string Insert_dash(string num_str)
{
string result_str = num_str;
// Traverse the string character
// by character
for (int x = 0;
x < num_str.length() - 1; x++) {
// Compare every consecutive
// character with the odd value
if (checkOdd(num_str[x])
&& checkOdd(num_str[x + 1])) {
result_str.insert(x + 1, "-");
num_str = result_str;
x++;
}
}
// Print the resultant string
return result_str;
}
// Driver Code
int main()
{
// Given number in form of string
string str = "1745389";
// Function Call
cout << Insert_dash(str);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to check if char ch is
// odd or not
static boolean checkOdd(char ch)
{
return ((ch - '0') & 1) != 0 ?
true : false;
}
// Function to insert dash - between
// any 2 consecutive digit in string str
static String Insert_dash(String num_str)
{
StringBuilder result_str = new StringBuilder(num_str);
// Traverse the string character
// by character
for(int x = 0; x < num_str.length() - 1; x++)
{
// Compare every consecutive
// character with the odd value
if (checkOdd(num_str.charAt(x)) &&
checkOdd(num_str.charAt(x + 1)))
{
result_str.insert(x + 1, "-");
num_str = result_str.toString();
x++;
}
}
// Print the resultant string
return result_str.toString();
}
// Driver Code
public static void main(String[] args)
{
// Given number in form of string
String str = "1745389";
// Function call
System.out.println(Insert_dash(str));
}
}
// This code is contributed by rutvik_56
Python3
# Python3 program for the above approach
# Function to check if char ch is
# odd or not
def checkOdd(ch):
return ((ord(ch) - 48) & 1)
# Function to insert dash - between
# any 2 consecutive digit in string str
def Insert_dash(num_str):
result_str = num_str
# Traverse the string character
# by character
x = 0
while(x < len(num_str) - 1):
# Compare every consecutive
# character with the odd value
if (checkOdd(num_str[x]) and
checkOdd(num_str[x + 1])):
result_str = (result_str[:x + 1] + '-' +
result_str[x + 1:])
num_str = result_str
x += 1
x += 1
# Print the resultant string
return result_str
# Driver Code
# Given number in form of string
str = "1745389"
# Function call
print(Insert_dash(str))
# This code is contributed by vishu2908
C#
// C# program to implement
// the above approach
using System;
using System.Text;
class GFG{
// Function to check if char ch is
// odd or not
static bool checkOdd(char ch)
{
return ((ch - '0') & 1) != 0 ?
true : false;
}
// Function to insert dash - between
// any 2 consecutive digit in string str
static String Insert_dash(String num_str)
{
StringBuilder result_str = new StringBuilder(num_str);
// Traverse the string character
// by character
for(int x = 0; x < num_str.Length - 1; x++)
{
// Compare every consecutive
// character with the odd value
if (checkOdd(num_str[x]) &&
checkOdd(num_str[x + 1]))
{
result_str.Insert(x + 1, "-");
num_str = result_str.ToString();
x++;
}
}
// Print the resultant string
return result_str.ToString();
}
// Driver Code
public static void Main(String[] args)
{
// Given number in form of string
String str = "1745389";
// Function call
Console.WriteLine(Insert_dash(str));
}
}
// This code is contributed by Rajput-Ji
JavaScript
// Javascript program for the above approach
// Function to check if char ch is
// odd or not
function checkOdd(ch)
{
return (parseInt(ch) & 1);
}
// Function to insert dash - between
// any 2 consecutive digit in string str
function Insert_dash(num_str)
{
let result_str = "";
// Traverse the string character
// by character
for (let x = 0;
x < num_str.length - 1; x++) {
// Compare every consecutive
// character with the odd value
if (checkOdd(num_str[x])
&& checkOdd(num_str[x + 1])) {
result_str+=(num_str[x]+ "-");
}
else{
result_str+=num_str[x];
}
}
result_str+=num_str[num_str.length-1];
// Print the resultant string
return result_str;
}
// Driver Code
// Given number in form of string
let str = "1745389";
// Function Call
document.write(Insert_dash(str));
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:
C++
// C++ Program to implement
// the above approach
#include <iostream>
#include <regex>
using namespace std;
// Function to insert dash - between
// any 2 consecutive odd digit
string Insert_dash(string str)
{
// Get the regex to be checked
const regex pattern("([13579])([13579])");
// Replaces the matched value
// (here dash) with given string
return regex_replace(str, pattern, "$1-$2");;
}
// Driver Code
int main()
{
string str = "1745389";
cout << Insert_dash(str);
return 0;
}
// This code is contributed by yuvraj_chandra
Java
// Java program for the above approach
import java.util.regex.*;
public class GFG {
// Function to insert dash - between
// any 2 consecutive odd digit
public static String Insert_dash(String str)
{
// Get the regex to be checked
String regex = "(?<=[13579])(?=[13579])";
// Create a pattern from regex
Pattern pattern = Pattern.compile(regex);
// Create a matcher for the input String
Matcher matcher
= pattern.matcher(str);
// Get the String to be replaced,
// i.e. here dash
String stringToBeReplaced = "-";
StringBuilder builder
= new StringBuilder();
// Replace every matched pattern
// with the target String
// using replaceAll() method
return (matcher
.replaceAll(stringToBeReplaced));
}
// Driver Code
public static void main(String[] args)
{
// Given number in form of string
String str = "1745389";
// Function Call
System.out.println(Insert_dash(str));
}
}
Python
# Python program for the above approach
import re
# Function to insert dash - between
# any 2 consecutive odd digit
def Insert_dash(str):
# Get the regex to be checked
regex = "(?<=[13579])(?=[13579])"
return re.sub(regex,'\1-\2', str)
# Driver Code
# Given number in form of string
str = "1745389"
# Function Call
print(Insert_dash(str))
# This code is contributed by yuvraj_chandra
C#
// C# Program to implement
// the above approach
using System;
using System.Text.RegularExpressions;
public class GFG {
// Function to insert dash - between
// any 2 consecutive odd digit
static string Insert_dash(string str)
{
// Get the regex to be checked
string pattern="([13579])([13579])";
// Replaces the matched value
// (here dash) with given string
return Regex.Replace(str, pattern, "$1-$2");;
}
// Driver Code
public static void Main()
{
string str = "1745389";
Console.WriteLine(Insert_dash(str));
}
}
// This code is contributed by Aman Kumar.
JavaScript
// Javascript Program to implement
// the above approach
// Function to insert dash - between
// any 2 consecutive odd digit
function Insert_dash(str)
{
// Get the regex to be checked
let regex = new RegExp("([13579])([13579])",'g');
// Replaces the matched value
// (here dash) with given string
return str.replace(regex, '$1-$2');
}
// Driver Code
let str = "1745389";
console.log(Insert_dash(str));
// This code is contributed by Pushpesh Raj.
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Program to delete Nth digit of a Number Given a number num and a number n, the task is to delete this nth digit of the number num, from starting and from end.Examples: Input: num = 1234, n = 3 Output: num_after_deleting_from_starting = 124, num_after_deleting_from_end = 134Input: num = 4516312, n = 2 Output: num_after_deleting_from_starti
15+ min read
C++ Program For Decimal To Octal Conversion The octal numbers are a base 8 number system that uses digits from 0-7 and the decimal numbers are a base 10 numbers system that uses 10 digits from 0-9 to represent any numeric value. In this article, we will learn how to write a C++ program to convert a given decimal number into an equivalent octa
2 min read
How to Add Leading Zeros to a C++ String? In C++, a string data structure is used to store the sequence of characters. These characters can be letters, symbols, or numbers. In this article, we will learn how to add leading zeros to a string that represents some numeric data in C++. Examples: Input: str="123" N=2 Output: "00123" Explanation:
2 min read
Check whether given floating point number is even or odd Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number
6 min read
Number formed by deleting digits such that sum of the digits becomes even and the number odd Given a non-negative number N, the task is to convert the number by deleting some digits of the number, such that the sum of the digits becomes even but the number is odd. In case there is no possible number, then print -1.Note: There can be multiple numbers possible for a given N.Examples: Input: N
5 min read
Program to convert float decimal to Octal number Write a program that converts a floating-point decimal number into its octal format. The program should take a number and the precision for the octal fractional part as inputs from the user. It should calculate and provide its correct octal representation as output. Examples: Input: Number = 123.45,
8 min read