用 C++ 將句子轉換成它等效的移動數字鍵盤序列
在本教程中,我們將討論程式,來將句子轉換為其等效的移動數字鍵盤序列。
為此,我們將提供一個由字母字元組成的字串。我們的任務是列印字串的數字等價,即鍵入特定字串的數字序列。
示例
#include <bits/stdc++.h>
using namespace std;
//computing the numerical sequence
string calc_sequence(string arr[], string input){
string output = "";
//length of input string
int n = input.length();
for (int i=0; i<n; i++){
//checking if space is present
if (input[i] == ' ')
output = output + "0";
else {
int position = input[i]-'A';
output = output + arr[position];
}
}
return output;
}
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 = "TUTORIALSPOINT";
cout << calc_sequence(str, input);
return 0;
}輸出
8888666777444255577777666444668
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP