📜  将给定的字符串转换为有效的手机号码

📅  最后修改于: 2021-09-02 06:40:40             🧑  作者: Mango

给定一个由字母、数字和符号组成的字符串M ,任务是通过删除除数字以外的所有字符,将字符串转换为有效的手机号码,格式如下:

  • 形成一个3位数的子串,剩余字符串的长度大于3
  • 用括号“()”将每个子字符串括起来,并用“-”分隔它们。

如果无法获得有效的手机号码,即如果字符串不是由 10 位数字组成,则打印-1 。否则,打印获得的字符串。

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个字符串,比如说S并按照给定的顺序将M 的所有数字追加到S中。
  • 现在,如果S的长度不是10 ,则打印“Invalid” ,并结束程序。
  • 否则,如果字符串S的长度为10 ,则组成一组3 个字符并将其括在方括号“()”中,并用“-”分隔。
  • 将最终字符串打印为S

下面是上述问题的解决方案。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the valid
// and formatted phone number
void Validate(string M)
{
    // Length of giiven string
    int len = M.size();
 
    // Store digits in temp
    string temp = "";
 
    // Iterate given M
    for (int i = 0; i < len; i++) {
        // If any digit, append it to temp
        if (isdigit(M[i]))
            temp += M[i];
    }
 
    // Find new length of string
    int nwlen = temp.size();
 
    // If length is not equal to 10
    if (nwlen != 10) {
        cout << "Invalid\n";
        return;
    }
 
    // Store final result
    string res = "";
 
    // Make groups of 3 digits and
    // enclose them within () and
    // separate them with "-"
    // 0 to 2 index 1st group
    string x = temp.substr(0, 3);
    res += "(" + x + ")-";
 
    // 3 to 5 index 2nd group
    x = temp.substr(3, 3);
    res += "(" + x + ")-";
 
    // 6 to 8 index 3rd group
    x = temp.substr(6, 3);
    res += "(" + x + ")-";
 
    // 9 to 9 index last group
    x = temp.substr(9, 1);
    res += "(" + x + ")";
 
    // Print final result
    cout << res << "\n";
}
 
// Driver Code
int main()
{
 
    // Given string
    string M = "91 234rt5%34*0 3";
 
    // Fuction Call
    Validate(M);
}
 
// contributed by ajaykr00kj


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to print the valid
// and formatted phone number
static void Validate(String M)
{
   
    // Length of giiven String
    int len = M.length();
 
    // Store digits in temp
    String temp = "";
 
    // Iterate given M
    for (int i = 0; i < len; i++)
    {
       
        // If any digit, append it to temp
        if (Character.isDigit(M.charAt(i)))
            temp += M.charAt(i);
    }
 
    // Find new length of String
    int nwlen = temp.length();
 
    // If length is not equal to 10
    if (nwlen != 10)
    {
        System.out.print("Invalid\n");
        return;
    }
 
    // Store final result
    String res = "";
 
    // Make groups of 3 digits and
    // enclose them within () and
    // separate them with "-"
    // 0 to 2 index 1st group
    String x = temp.substring(0, 3);
    res += "(" + x + ")-";
 
    // 3 to 5 index 2nd group
    x = temp.substring(3, 6);
    res += "(" + x + ")-";
 
    // 6 to 8 index 3rd group
    x = temp.substring(6, 9);
    res += "(" + x + ")-";
 
    // 9 to 9 index last group
    x = temp.substring(9, 10);
    res += "(" + x + ")";
 
    // Print final result
    System.out.print(res+ "\n");
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Given String
    String M = "91 234rt5%34*0 3";
 
    // Fuction Call
    Validate(M);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to prthe valid
# and formatted phone number
def Validate(M):
     
    # Length of giiven
    lenn = len(M)
 
    # Store digits in temp
    temp = ""
 
    # Iterate given M
    for i in range(lenn):
         
        # If any digit:append it to temp
        if (M[i].isdigit()):
            temp += M[i]
 
    # Find new lenngth of
    nwlenn = len(temp)
 
    # If lenngth is not equal to 10
    if (nwlenn != 10):
        print ("Invalid")
        return
 
    # Store final result
    res = ""
 
    # Make groups of 3 digits and
    # enclose them within () and
    # separate them with "-"
    # 0 to 2 index 1st group
    x = temp[0:3]
    res += "(" + x + ")-"
 
    # 3 to 5 index 2nd group
    x = temp[3 : 3 + 3]
    res += "(" + x + ")-"
 
    # 6 to 8 index 3rd group
    x = temp[6 : 3 + 6]
    res += "(" + x + ")-"
 
    # 9 to 9 index last group
    x = temp[9 : 1 + 9]
    res += "(" + x + ")"
 
    # Print final result
    print(res)
 
# Driver Code
if __name__ == '__main__':
     
    # Given
    M = "91 234rt5%34*0 3"
 
    # Function Call
    Validate(M)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG
{
 
  // Function to print the valid
  // and formatted phone number
  static void Validate(string M)
  {
 
    // Length of giiven String
    int len = M.Length;
 
    // Store digits in temp
    string temp = "";
 
    // Iterate given M
    for (int i = 0; i < len; i++)
    {
 
      // If any digit, append it to temp
      if (Char.IsDigit(M[i]))
        temp += M[i];
    }
 
    // Find new length of String
    int nwlen = temp.Length;
 
    // If length is not equal to 10
    if (nwlen != 10)
    {
      Console.Write("Invalid\n");
      return;
    }
 
    // Store final result
    string res = "";
 
    // Make groups of 3 digits and
    // enclose them within () and
    // separate them with "-"
    // 0 to 2 index 1st group
    string x = temp.Substring(0, 3);
    res += "(" + x + ")-";
 
    // 3 to 5 index 2nd group
    x = temp.Substring(3, 3);
    res += "(" + x + ")-";
 
    // 6 to 8 index 3rd group
    x = temp.Substring(6, 3);
    res += "(" + x + ")-";
 
    // 9 to 9 index last group
    x = temp.Substring(9, 1);
    res += "(" + x + ")";
 
    // Print final result
    Console.WriteLine(res);
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
 
    // Given String
    string M = "91 234rt5%34*0 3";
 
    // Fuction Call
    Validate(M);
  }
}
 
// This code is contributed by AnkThon


输出:
(912)-(345)-(340)-(3)

时间复杂度: O(|M|+|S|)
辅助空间: O(|M|+|S|)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live