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

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:



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

1-745-389


 

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

Rgular 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:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output: 

1-745-389


 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vishu2908, rutvik_56, Rajput-Ji