so i need a function that gets a char and puts out a number
ex.
a=0
b=1
and so on.
i have looked into ascii codes,but a is 97,and it is necessary for it to be 0.if i just take 97 97 out of the ascii number,it gives errors for spaces and such
the way i have it now is just one big switch statement.
int ToChar(int x){
switch(x){
case 0:
return'a';
break;
case 1:
return'b';
break;
case 2:
return'c';
break;
case 3:
return'd';
break;
case 4:
return'e';
break;
case 5:
return'f';
break;
case 6:
return'g';
break;
case 7:
return'h';
break;
case 8:
return'i';
break;
case 9:
return'j';
break;
case 10:
return'k';
break;
case 11:
return'l';
break;
case 12:
return'm';
break;
case 13:
return'n';
break;
case 14:
return'o';
break;
case 15:
return'p';
break;
case 16:
return'q';
break;
case 17:
return'r';
break;
case 18:
return's';
break;
case 19:
return't';
break;
case 20:
return'u';
break;
case 21:
return'v';
break;
case 22:
return'w';
break;
case 23:
return'x';
break;
case 24:
return'y';
break;
case 25:
return'z';
break;
default:
break;
}
return 0;
}
It returns the ASCII because the argument is an integer and int can't hold a letter only the integers corresponding to that letter according to ASCII.
(if that was your question)
no that isn't my problem.
that was just some idea i had,if i get the ascii number it is easy to get the letter.
my question is if there is a better way to code the function you see above,cause it is ugly imo,and i have no ideas left on how to make it better.
The techniques being discussed only work if using a character set based on ASCII. This is admittedly most of them. However, it will not work with EBCDIC. I would put a comment in the function headers which indicates that it is not portable code.
As PCrumley48 mentions, this only works with lowercase. What do you want or need to happen with 'A'?
by not portable,do you mean to other operating systems or what?
would my original function be portable,compared to this one(which i am now using?).
also upper case is converted to lowercase and then passed through the function,numbers and special characters are converted to 'x'.
i know how to do that though so thanks to all of you :)
by not portable,do you mean to other operating systems or what?
Yes, I mean a computer where the operating system is not using an ASCII based character set. That probably means IBM mainframes which use (or did use) EBCDIC characters.
would my original function be portable,compared to this one(which i am now using?).
Yes, rereading your original, it seems to be portable.
But the fundamental question for many projects, is portability a goal of the project?