如何将字符更改为数字?
How do i change char to number?
我需要将某些字符更改为数字,例如:
I = 1
R = 2
E = 3
A = 4
S = 5
G = 6
T = 7
B = 8
P = 9
O = 0
输入示例:你
好我好输出示例:H3LL0 1M G00D
你想让我们做你的功课吗?
无论如何,有多种可能性。
- 对于初学者 - 最基本的方法是遍历字符串并用新的字符替换每个需要的字符(您可以使用开关大小写、查找表等)。
-
您可以转换为字符串并按以下方式使用它的方法:
string s; s="HELLO IM GOOD" s.replace('I,'1') s.replace('R,'2') . . . cout << s; //print solution
这段代码很有帮助:)
#include <iostream>
#include<stdio.h>
using namespace std;
int main() {
string s;
getline (cin, s); //used to get string input with spaces
string s1 = "OIREASGTBP";
string s2 = "0123456789";
for (int i=0; i<s.size(); i++)
{
int a = s1.find(s[i]);
if(a != -1)
{
s[i] = s2[a];
}
}
cout<<s;
}
相关文章:
- 递归形成字符串中所有数字字符的中间和?
- C++ 从 std::string 中删除所有非数字字符
- 如何返回一串字母数字字符中的最大值(例如-1A003B3)?
- 有没有办法将最初作为字符串输入的数字字符数组复制到双向链表中
- 如何使用 c++ 将数字字符转换为字符串
- 从字符数组中修剪非字母数字字符的功能
- 使用find_if和isalnum在字符串中找到字母数字字符
- 循环直到整数输入在所需范围内无法处理非数字字符输入
- 确保字符串只包含字母数字字符的更好方法
- 用C++计算文本文件中的字母数字字符
- 使用cctype库搜索函数,以查找某个范围内的数字字符数
- 在读取C++行时过滤非字母数字字符
- C++ 如何从标准输入加载到最多 5 个字母数字字符的字符数组
- 如何知道小部件是否只接受数字字符
- 在 C++ STL 中,如何使用 regex_replace 从 std::string 中删除非数字字符
- i18n 去除非字母数字字符的友好方式
- 将字符数组拆分为字母数字字符和非字母数字字符
- 将多数字字符数转换为整数
- 在检测字符串是否包含数字字符时,这有什么问题
- 如何检测QString是否由所有数字字符组成