字符串~键盘错位



 1 //------------C_Style-----------------
 2 
 3 #include<stdio.h>
 4 
 5 void main()
 6 {
 7     char *str = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
 8     int c;
 9     printf("Enter a string(Ctr+Z to end):\n");
10     while((c = getchar()) != EOF)
11     {
12         for(int ix = 1; str[ix] && str[ix] != c; ++ix);
13             if(str[ix])
14                 putchar(str[ix-1]);
15             else
16                 putchar(c);
17     }
18 }
 1 //------------C++_Style-----------------
 2 
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 
 7 string str = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
 8 void main()
 9 {
10     string s;
11     cout << "Enter a string(Ctr+Z to end):" << endl;
12     getline(cin, s);
13     for(string::size_type ix = 0; ix != s.size(); ++ix)
14     {
15         string::size_type index = 1;
16         for(; index != str.size() && str[index] != s[ix]; ++index);
17         if(index != str.size())
18             s[ix] = str[index-1];
19     }
20     cout << s << endl;
21 }
/*
比较以上两个代码,对于这类低级的题目C字符串风格char给适合写……
另外,对于小规模的题目,可以使用本题的常量数组,把所有可能情况都列出来,与问题相比较处理,这样更简洁……
*/
原文地址:https://www.cnblogs.com/sanghai/p/2759358.html