Reputation: 23
I was intending to create a simple function that would take a string as its input and output the equivalent of that string in ASCII. Plz help..
void cls(){
system("cls");
}
void getAscii(){
cls();
text(4);
char a[94]={' ','!','"','#','$','%','&',"'",'(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@',
'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','[',"'\'",']','^','_','`','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','{','|','}','~'};
while(1){
char x[5000], *exitMsg = "quit";
gets(x);
if(strcmp(x, exitMsg) == 0){
break;
}else{
int i = 0;
for(i = 0; i < strlen(x); i++){
int j = 0;
for(j = 0; j < 94; j++){
if(x[i] == a[j]){
int xa = (a[j] + 32);
printf("%d", &xa);
}
}
}
printf("\n");
}
}
}
Upvotes: 1
Views: 189
Reputation: 2757
There are a few errors in your code :
The corrected code is:
#include<stdio.h>
#include<process.h>
#include<string.h>
void cls(){
system("cls");
}
void Ascii(){
cls();
// text(4); //uncomment this if it does something useful
char a[95]={' ','!','"','#','$','%','&',' ','(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@',
'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','[','\\',']','^','_','`','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','{','|','}','~'};
while(1){
char x[5000], *exitMsg = "quit";
gets(x);
if(strcmp(x, exitMsg) == 0){
break;
}else{
int i = 0;
for(i = 0; i < strlen(x); i++){
int j = 0;
for(j = 0; j < 94; j++){
if(x[i] == a[j]){
int xa = (a[j] );
printf("%d ", xa);
}
}
}
printf("\n");
}
}
}
ALTHOUGH You require none of this.
Try this:
void cls(){
system("cls");
}
void Ascii(){
cls();
while(1){
char x[5000], *exitMsg = "quit";
gets(x);
if(strcmp(x, exitMsg) == 0){
break;
}else{
int i = 0;
for(i = 0; i < strlen(x); i++){
int xa = (x[i] );
printf("%d ", xa);
}
}
printf("\n");
}
}
Upvotes: 1
Reputation: 369
A char
is just an one byte number. When it represents an ascii character it is actually just the number for it. For example when you say char x = 'A'
, you are essentially saying char x = 65
. The one byte in memory representing x
really stores the number 65
. If you did x+1
you would get 66
or 'B'
depending on how you print it. When you tell it to print a char it will look up the ascii table and print the character. If you tell it to print a decimal it will print 65
. For example:
char x = 'A';
printf("%d", x);
This will print 65
. You do not need a conversion table to look up the ascii values.
No need for ascii arrays and another loop inside your code.
This
for(i = 0; i < strlen(x); i++){
int j = 0;
for(j = 0; j < 94; j++){
if(x[i] == a[j]){
int xa = (a[j] + 32);
printf("%d", &xa);
}
}
}
can be simplified to
for(i = 0; i < strlen(x); i++) {
printf("%d", x[i]);
}
Upvotes: 3