如何将字符更改为数字?

How do i change char to number?

本文关键字:数字 字符      更新时间:2023-10-16

我需要将某些字符更改为数字,例如:

I = 1

R = 2

E = 3

A = 4

S = 5

G = 6

T = 7

B = 8

P = 9

O = 0

输入示例:你

好我好输出示例:H3LL0 1M G00D

你想让我们做你的功课吗?

无论如何,有多种可能性。

  1. 对于初学者 - 最基本的方法是遍历字符串并用新的字符替换每个需要的字符(您可以使用开关大小写、查找表等)。
  2. 您可以转换为字符串并按以下方式使用它的方法:

    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;
}