Convierta una oración en su secuencia de teclado numérico móvil equivalente

Dada una oración en forma de string, conviértala en su secuencia de teclado numérico móvil equivalente. 
 

Mobile-keypad

C++

// C++ implementation to convert a
// sentence into its equivalent
// mobile numeric keypad sequence
#include <bits/stdc++.h>
using namespace std;
  
// Function which computes the sequence
string printSequence(string arr[],
                       string input)
{
    string output = "";
  
    // length of input string
    int n = input.length();
    for (int i=0; i<n; i++)
    {
        // Checking for space
        if (input[i] == ' ')
            output = output + "0";
  
        else
        {
            // Calculating index for each 
            // character
            int position = input[i]-'A';
            output = output + arr[position];
        }
    }
  
    // Output sequence
    return output;
}
  
// Driver function
int main()
{
    // storing the sequence in array
    string str[] = {"2","22","222",
                    "3","33","333",
                    "4","44","444",
                    "5","55","555",
                    "6","66","666",
                    "7","77","777","7777",
                    "8","88","888",
                    "9","99","999","9999"
                   };
  
    string input = "GEEKSFORGEEKS";
    cout << printSequence(str, input);
    return 0;
}

Java

// Java implementation to convert a
// sentence into its equivalent
// mobile numeric keypad sequence
import java.util.*;
  
class GFG
{
  
    // Function which computes the sequence
    static String printSequence(String arr[],
                               String input)
    {
        String output = "";
      
        // length of input string
        int n = input.length();
        for (int i = 0; i < n; i++)
        {
            // Checking for space
            if (input.charAt(i) == ' ')
                output = output + "0";
      
            else
            {
                // Calculating index for each 
                // character
                int position = input.charAt(i) - 'A';
                output = output + arr[position];
            }
        }
      
        // Output sequence
        return output;
    }
      
    // Driver Function
    public static void main(String[] args)
    {
        // storing the sequence in array
        String str[] = {"2","22","222",
                        "3","33","333",
                        "4","44","444",
                        "5","55","555",
                        "6","66","666",
                        "7","77","777","7777",
                        "8","88","888",
                        "9","99","999","9999"
                    };
  
        String input = "GEEKSFORGEEKS";
        System.out.println(printSequence(str, input));
    }
}
  
// This code is contributed by Gitanjali.

Python3

# Python3 implementation to convert
# a sentence into its equivalent
# mobile numeric keypad sequence
  
# Function which computes the 
# sequence
def printSequence(arr, input):
  
# length of input string
    n = len(input)
    output = ""
      
    for i in range(n):
      
        # checking for space
        if(input[i] == ' '):
            output = output + "0"
        else:
            # calculating index for each
            # character         
            position = ord(input[i]) - ord('A')
            output = output + arr[position]
    # output sequence     
    return output
      
# Driver code
str = ["2", "22", "222",
       "3", "33", "333",
       "4", "44", "444",
       "5", "55", "555",
       "6", "66", "666",
       "7", "77", "777", "7777",
       "8", "88", "888",
       "9", "99", "999", "9999" ]
  
input = "GEEKSFORGEEKS";
print( printSequence(str, input))
  
# This code is contributed by upendra bartwal

C#

// C# implementation to convert a
// sentence into its equivalent
// mobile numeric keypad sequence
using System;
  
class GFG
{
  
    // Function which computes the sequence
    static String printSequence(string []arr,
                            string input)
    {
        string output = "";
      
        // length of input string
        int n = input.Length;
        for (int i = 0; i < n; i++)
        {
            // Checking for space
            if (input[i] == ' ')
                output = output + "0";
      
            else
            {
                // Calculating index for each 
                // character
                int position = input[i] - 'A';
                output = output + arr[position];
            }
        }
      
        // Output sequence
        return output;
    }
      
    // Driver Function
    public static void Main()
    {
        // storing the sequence in array
        string []str = {"2","22","222",
                        "3","33","333",
                        "4","44","444",
                        "5","55","555",
                        "6","66","666",
                        "7","77","777","7777",
                        "8","88","888",
                        "9","99","999","9999"
                    };
  
        string input = "GEEKSFORGEEKS";
        Console.WriteLine(printSequence(str, input));
    }
}
  
// This code is contributed by vt_m.

PHP

<?php 
// PHP implementation to convert a
// sentence into its equivalent
// mobile numeric keypad sequence
  
// Function which computes the sequence
function printSequence(&$arr, $input)
{
    $output = "";
  
    // length of input string
    $n = strlen($input);
    for ($i = 0; $i < $n; $i++)
    {
        // Checking for space
        if ($input[$i] == ' ')
            $output = $output + "0";
  
        else
        {
            // Calculating index for each 
            // character
            $position = ord($input[$i]) - ord('A');
            $output = $output . $arr[$position];
        }
    }
  
    // Output sequence
    return $output;
}
  
// Driver Code
  
// storing the sequence in array
$str = array("2","22","222", "3","33","333",
             "4","44","444", "5","55","555",
             "6","66","666", "7","77","777","7777",
             "8","88","888", "9","99","999","9999");
  
$input = "GEEKSFORGEEKS";
echo printSequence($str, $input);
  
// This code is contributed by ita_c
?>

Javascript

<script>
  
// Javascript implementation to convert a
// sentence into its equivalent
// mobile numeric keypad sequence
      
    // Function which computes the sequence
    function printSequence(arr,input)
    {
        let output = "";
        
        // length of input string
        let n = input.length;
        for (let i = 0; i < n; i++)
        {
            // Checking for space
            if (input[i] == ' ')
                output = output + "0".charCodeAt(0);
        
            else
            {
                // Calculating index for each 
                // character
                let position = input[i].charCodeAt(0) - 'A'.charCodeAt(0);
                output = output + arr[position];
            }
        }
        
        // Output sequence
        return output;
    }
      
    // Driver Function
    let str = ["2", "22", "222",
       "3", "33", "333",
       "4", "44", "444",
       "5", "55", "555",
       "6", "66", "666",
       "7", "77", "777", "7777",
       "8", "88", "888",
       "9", "99", "999", "9999" ]
      
    let input = "GEEKSFORGEEKS";
    document.write(printSequence(str, input));
      
    // This code is contributed by avanitrachhadiya2155
</script>

Publicación traducida automáticamente

Artículo escrito por parthendo y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *