Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

แปลงประโยคเป็นลำดับตัวเลขคีย์พัดมือถือ (Mobile Numeric Keypad Sequence) ใน 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