题目:手机的九宫格输入,2上是“ABC”,“9”对应“WXYZ”,现假设“1”和“0”为空字符,根据手机上的输出,判断手机输入了哪几个数字。
要求:
输入 字符串s代表手机上的输出。
输出 依次输出手机上输入的数字。
思路:
1、创建一个2-9对应字符的数组,使用迭代器遍历查找。
2、可以用string.find()来查找,迭代器是一个指针,使用string的方法是要(*iterator)。(可见编程练习(1))
通过的代码:
1、直接遍历
tolower()函数是将大写字母变为小写,如果是小写则不变。
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<string> samp = { "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" };
string strt;
int ifhas=0;
getline(cin, strt);
for (int j = 0; j < strt.length(); j++)
{
strt[j] = tolower(strt[j]);
for (auto i = samp.begin(); i != samp.end(); i++)
{
for (auto p = (*i).begin(); p != (*i).end(); p