For example:
Input: Onetwothree
Output: 123
Input: Onetwodoubletwo
Output: 1222
Input: 1TWO2
Output: ERROR
Input: Doubledoubletwo
Output: ERROR
There are spaces, illegal characters, two double connections, double in the last word is wrong.
#include <iostream> #include <string> using namespace std; void process (String str) {//the subscript of each string is its corresponding Arabic numeral char *ch[]={"zero", "one", "one", "three", "four", "five", "six "," Seven "," eight "," Nine "," Double "};char *out[100];//store result set int f=0; int i,j,k; String substr; int Len=str.length (); int flag=0;//judge Double appears int flag2=0;//The inner loop has no break for (i=0;i<len;i++) {if (Flag2) break;else if (str[i]<97 | | str[i]>122)//non-alphabetic character {out[f++]= "error"; break;} else{//can be seen with a minimum of 3 characters and a maximum of 6 characters for (j=i+2;j<len&& (j-i) <6;j++) {substr=str.substr (i,j-i+1); if (!substr.compare (CH[10))//appears double{if (i==len-6)//Last double{out[f++]= "error"; flag2=1;break;} else if (flag) {//two consecutive doubleout[f++]= "error"; flag2=1;break;} Elseflag=1;} Else{for (k=0;k<10;k++) {if (!substr.compare (Ch[k])) {if (flag) {out[f++]=ch[k];out[f++]=ch[k];flag=0;break;} ELSE{OUT[F++]=CH[K]; Break;}} }}}}} int flag3=0;//See if there is an error, the default is no errorfor (int r=0;r<f;r++) {if (out[r]== "error")//has errorflag3=1;} if (!FLAG3) for (r=0;r<f;r++) for (k=0;k<10;k++) {if (Out[r]==ch[k]) printf ("%d", k);} elseprintf ("error"); printf ("\ n"); } void Main () {string str; while (Getline (CIN,STR)) process (str); }
Huawei Machine test-translate phone number