sanghai


 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给适合写……
另外,对于小规模的题目,可以使用本题的常量数组,把所有可能情况都列出来,与问题相比较处理,这样更简洁……
*/

分类:

技术点:

相关文章:

  • 2021-10-01
  • 2022-01-23
  • 2021-06-08
  • 2021-10-06
  • 2021-11-05
  • 2021-12-22
  • 2021-07-18
  • 2021-09-05
猜你喜欢
  • 2021-12-25
  • 2021-09-19
  • 2021-11-02
  • 2021-11-17
  • 2021-10-07
  • 2021-06-08
  • 2021-11-20
相关资源
相似解决方案