如何将5个随机ascii值转换为字符?
提示:
从97到122随机生成5个ascii值(所有字母表的ascii值)。当你去的时候,确定与每个ascii值对应的字母,并输出由5个字母组成的单词。
我的代码:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main ()
{
srand (time(NULL));
int val1= rand()%122+97;
int val2= rand()%122+97;
int val3= rand()%122+97;
int val4= rand()%122+97;
int val5= rand()%122+97
cout<<val1<<" and "<<val2<<" and "<<val3<<" and "<<val4<<" and "<<val15<<". "<<
return 0;
}
答案 0 :(得分:11)
for (int i = 0; i < 5; i++){
int asciiVal = rand()%26 + 97;
char asciiChar = asciiVal;
cout << asciiChar << " and ";
}
答案 1 :(得分:4)
要将int
ASCII值转换为字符,您还可以使用:
int asciiValue = 65;
char character = char(asciiValue);
cout << character; // output: A
cout << char(90); // output: Z