与更换使用C数字字母++ [英] replacing letters with numbers using C++
本文介绍了与更换使用C数字字母++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要更换与数字我输入的字母。结果 A = 00
, B = 01
, C = 02
等等......结果
我觉得有什么不妥焦炭ENC,程序不工作时 CH =='J'
或更高。
的#include<&stdio.h中GT;
#包括LT&;&CONIO.H GT;
#包括LT&;&string.h中GT;
#包括LT&;&WINDOWS.H GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&文件ctype.h GT;诠释主(){
焦炭CH ='G'; //这应该以某种输入功能的替换
焦炭ALP [26] = {'A','B','C','D','E','F','G','H','我','J','K' ,'L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y ','Z'};
炭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;我++)
如果(CH == ALP [I]){
的printf(%C,ENC [I]);
打破;
} 而(getchar函数()='\\ n'!);
返回0;
}
解决方案
我建议你完全重写你的code:
#包括LT&;&iostream的GT;
#包括LT&;限制和GT;
诠释主(){
字符输入=的std :: cin.get() - 'A'; // 1
如果(输入&10 9)
性病::法院LT&;< 0;
性病::法院LT&;< INT(输入)LT;<\\ n \\ n preSS Enter退出程序。 // 2
的std :: cin.sync(); // 3
的std :: cin.ignore(的std :: numeric_limits<&streamsize可GT; :: MAX()的'\\ n'); // 4
}
说明:
-
的std :: cin.get()
返回摘自第一个字符的标准输入
的 -
字符
是一个数字。 这里你可以检查哪些看重它为每一个字符。 - 你可以看到,字母排列此起彼伏,按字母顺序排列。所以,当你写
'一' - '一'
你'\\ 0'
(字符与$ C $ ç0
)的'b' - '一'
你'\\ 1'
等
-
- 您想
的std :: COUT
来打印出人物的code,不是字符。要做到这一点,你必须投字符任何整数类型。 - 刷新的
标准输入
的 - 从丢弃字符的
标准输入
的,直到遇见<大骨节病>输入骨节病>。
I need to replace my input letters with numbers.
a = 00
, b = 01
, c = 02
and so on...
I think there is something wrong with char enc, program doesnt work when ch == 'j'
or higher.
#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;
}
解决方案
I'd recommend you to completely rewrite your code:
#include<iostream>
#include<limits>
int main(){
char input=std::cin.get()-'a'; //1
if(input<9)
std::cout<<0;
std::cout<<int(input)<<"\n\nPress Enter to exit program. "; //2
std::cin.sync(); //3
std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n');//4
}
Explanation:
-
std::cin.get()
returns first character extracted fromstdin
.char
is a number. Here you can check which value it has for every character.- as you can see, letters are arranged one after another, in alphabetical order. So, when you write
'a'-'a'
you get'\0'
(character with code0
) for'b'-'a'
you get'\1'
etc.
- You want
std::cout
to print out character's code, not character. To do this, you have to cast character to any integer type. - Flushes
stdin
. - Discards characters from
stdin
, until meets Enter.
这篇关于与更换使用C数字字母++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文