使用C++将字母替换为数字
replacing letters with numbers using C++
我需要用数字替换输入的字母a = 00
、b = 01
、c = 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'
等等
- 您希望
std::cout
打印出字符的代码,而不是字符。要做到这一点,您必须将字符强制转换为任何整数类型 - 冲洗
stdin
- 丢弃
stdin
中的字符,直到满足输入
相关文章:
- 如何在C ++中替换整数的前两位数字?
- 我有模板功能.通过使用函数std::for_each,用这个容器中的最大数字替换每个正数
- 使用 Get 的 C++ 输入被替换为空格、不正确的数字
- 在 Python/C++ 中用更改文件中的随机数替换数字
- TicTacToe代码不会用O和X替换数字
- 这个给定的代码应该将给定的数字转换为尽可能滞后的数字,no.by 用 9.It 替换合适的数字是行不通的
- 使用 sprintf 和 sscanf 将给定数字中的所有 0 替换为 5
- 我无法用字符替换重复的数字"a"
- 用递增的数字替换字符串中的空格
- 如何用C 中的数字替换字母
- C 如果指示指向NULLPTR,我可以用数字替换它
- 如何将向量中的所有元素替换为元素数字的总和
- 将两个数字替换为给定数组中介于两个之间的单个数字
- 二叉树实现 - 分支上的数字被替换
- 使用 C++ 替换 txt 文件中的文本数字字符串
- 检测几乎为零并将其替换为 0.0 或将它们保留为小数字是否更快?
- C++11正则表达式:在替换字符串中捕获组后的数字
- 如何将 std::string 中的所有非字母字符(数字和特殊字符)替换为空格
- 我应该用什么替换数字
- c++随机数组-需要用0替换数字22和33