Help make the program in C.
A1 = {A,B,C,D,E,F,G,H,I,J}; A2 = {0,1,2,3,4,5,6,7,8,9};
The function replaces the characters of a string from one given alphabet with the characters of another alphabet.
@atticus so?
#include <iostream> using namespace std; int main() { string s; while(!cin.eof()) { cin >> s; for(string::iterator it = s.begin(); it!=s.end(); it++) { if(*it < 'A' || *it > 'J') { cout << "Неверный символ " << *it << endl; return 0; } *it -= 'A' - '0'; } cout << s; } return 0; }
for (int i = 0; i < 10; i++) if (x == a[i]) return b[i]
-> if (x == a[0]) return b[0]; if (x == a[1]) return b[1]; ...
if (x == a[0]) return b[0]; if (x == a[1]) return b[1]; ...
if (x == a[0]) return b[0]; if (x == a[1]) return b[1]; ...
2. if (x == a[0])
-> if (x == 'A')
3. if
's sequence -> switch 4. Minimize the range to switch (default optimization in Java, for example): if (arg outside range) default; else jump table[arg]
if (arg outside range) default; else jump table[arg]
5. inline case'y 6. we take out the general subexpression for jump 7. we see that now all cases are the same, and we collapse them. - VladDSource: https://ru.stackoverflow.com/questions/227775/
All Articles
a1a2[(unsigned char) c]
- alexlzbzero()
IMHO, instead ofbzero()
, it is better to initialize with the codes themselves: for (i = 0; i <256; i ++) a1a2 [i] = i; for (i = 0; i <sizeof A1; i ++) a1a2 [A1 [i]] = A2 [i]; then the further code conversion code will be completely trivial. - avp