|
发表于 2020-4-8 13:16:38
|
显示全部楼层
如果非要用 string 类型的话,那就得改成这样才行:
- bool Str_Exch(string* str, int str_len, bool b_odd_pos = true)
- {
- cout << *str << '|' << str_len << '|' << b_odd_pos << endl;
- for (int i = 0; i < str_len; ++i)
- {
- int idx = i + 1;
- char& ch = (*str)[i];
- if ((b_odd_pos && idx % 2 != 0) || (!b_odd_pos && idx % 2 == 0))
- {
- if (ch >= 'a' && ch <= 'z')
- {
- ch -= 32; //小写字母和大写字母的ASCII码差了32
- }
- }
- }
- return 0;
- }
- int main()
- {
- string x = "abcdefghijklmnopq";
- Str_Exch(&x, x.size(), true);
- system("pause");
- return 0;
- }
复制代码 |
|