使用C++将字母替换为数字

replacing letters with numbers using C++

本文关键字:替换 数字 C++ 使用      更新时间:2023-10-16

我需要用数字替换输入的字母
a = 00b = 01c = 02等…
我认为char-enc有问题,程序在ch == 'j'或更高版本时不工作。

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
    char ch = 'g'; // this should be replaced with some kind of an input function
    char alp[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
          'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    char enc[26] = {'00', '01', '02', '03', '04', '05', '06', '07', '08', '09',
                    '10', '11', '12', '13', '14', '15', '16', '17', '18', '19',
                    '20', '21', '22', '23', '24', '25'};
    for(int i = 0; i <= 25; i++)
        if(ch == alp[i]){
            printf("%c", enc[i]);
            break;
        }
    while(getchar()!='n');
    return 0;
}

根据C++标准

包含以下内容的多字符文字或普通字符文字在执行字符集中不可表示的单个c-char是有条件支持,类型为int实现定义的值。

因此,最好定义一个指向字符串文字的指针数组,而不是第二个chatracter数组。请注意,定义std::string类型的对象数组没有任何意义。

同样,当程序看起来像是用C编写的时候,你为什么要谈论C++也不清楚。

C++中的代码可能看起来像

#include <iostream>
#include <cstdlib>
int main()
{
    size_t N = 26;
    char ch = 'g'; // this should be replaced with some kind of an input function
    const char alp[N] =
    {
       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 
       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    };
    const char *enc[N] = 
    {
        "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", 
        "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"
    };
    size_t i = 0;
    while ( i < N &&  ch != alp[i] ) i++;
    if ( i != N ) std::cout << enc[i] << std::endl;
    std::system( "PAUSE" );
    return 0;
}

用C编写的相同代码可能看起来像

#include <stdio.h>
#include <stdlib.h>
#define N 26
int main( void )
{
    char ch = 'g'; // this should be replaced with some kind of an input function
    const char alp[N] =
    {
       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 
       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    };
    const char *enc[N] = 
    {
        "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", 
        "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"
    };
    size_t i = 0;
    while ( i < N &&  ch != alp[i] ) i++;
    if ( i != N ) printf( "%sn", enc[i] );
    system( "PAUSE" );
    return 0;
}

这些不是C++字符:'00','01',。。。因为它们实际上包含两个字符。尝试使用这个:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
using namespace std;
int main()
{
char ch = 'g'; // this should be replaced with some kind of an input function
char alp[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
string enc[26] = {"00","01","02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"};
for(int i = 0; i <= 25; i++){
    if(ch == alp[i]){
        cout<<enc[i];
    }
}
system("PAUSE");
return 0;
}

我建议您使用std::map来满足您的需求。映射存储键值对。在您的情况下,键将是单个字符,值将是需要替换的字符串。这是的图解

#include <iostream>
#include <map>
using namespace std;
int main ()
{
char ch = 'b'; // this should be replaced with some kind of an input function
char alp[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
string enc[26] = {"00","01","02" /*and the rest*/};
// declare a map
map<char, string> valueDecoderMap;
// store the values in key,value form
for ( int i = 0; i < (sizeof(alp)); i++ )
{
   valueDecoderMap [ alp [ i ] ] = enc [ i ];
}
// Now search for particular value
map<char, string>::iterator mapIterator;
mapIterator = valueDecoderMap.find ( ch );
if ( mapIterator != valueDecoderMap.end () )
{
   cout << "Key = " << mapIterator->first << " Value = " << mapIterator->second << endl;
}
else
{
   cout << "No encoding present for " << ch << endl;
}
return 0;
}

这是一种更加面向c++的方法。希望这能有所帮助。

我建议您完全重写代码:

#include<iostream>
#include<limits>
int main(){
    char input=std::cin.get()-'a';                                //1
    if(input<9)
        std::cout<<0;
    std::cout<<int(input)<<"nnPress Enter to exit program. ";   //2
    std::cin.sync();                                              //3
    std::cin.ignore(std::numeric_limits<streamsize>::max(), 'n');//4
}

说明:

    • std::cin.get()返回从stdin中提取的第一个字符
    • char是一个数字。在这里,您可以检查每个字符的值
    • 正如你所看到的,字母是按字母顺序排列的。所以,当你写'a'-'a'时,你会得到''(代码为0的字符),而'b'-'a'则得到'1'等等
  1. 您希望std::cout打印出字符的代码,而不是字符。要做到这一点,您必须将字符强制转换为任何整数类型
  2. 冲洗stdin
  3. 丢弃stdin中的字符,直到满足输入