在C ++中将句子转换为其等效的移动数字小键盘序列
在本教程中,我们将讨论将句子转换为等效的移动数字小键盘序列的程序。
为此,我们将提供一串字母字符。我们的任务是打印与字符串等效的数字,即键入特定字符串的键的数字序列。
示例
#include <bits/stdc++.h>
using namespace std;
//计算数字序列
string calc_sequence(string arr[], string input){
string output = "";
//输入字符串的长度
int n = input.length();
for (int i=0; i<n; i++){
//检查是否存在空间
if (input[i] == ' ')
output = output + "0";
else {
int position = input[i]-'A';
output = output + arr[position];
}
}
return output;
}
int main(){
//将序列存储在数组中
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 = "nhooo";
cout << calc_sequence(str, input);
return 0;
}输出结果
8888666777444255577777666444668