📜  程序在给定Number的两个相邻奇数之间插入破折号

📅  最后修改于: 2021-05-06 23:18:13             🧑  作者: Mango

给定大量的字符串N ,任务是在字符串形式的给定数字的两个相邻奇数位数之间插入破折号。

例子:

按位方法:

  1. 横越字符数字字符的整个字符串。
  2. 使用逻辑按位OR和AND运算符比较每个连续的字符。
  3. 如果字符串的两个连续字符是奇数,请在其中插入破折号(-)并检查接下来的两个连续字符。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#include 
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


C++
// C++ Program to implement
// the above approach
#include 
#include 
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


输出:
1-745-389

时间复杂度: O(N)
辅助空间: O(1)

规则表达法:

给定的问题可以使用正则表达式解决。此问题的RE将是:

下面是上述方法的实现:

C++

// C++ Program to implement
// the above approach
#include 
#include 
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
输出:
1-745-389