c - Change letter with number -
i'm doing exercise, practice, point receive string of numbers , letters, check if it's letter, transforme number rule
a = 10, z = 35
and place in array. operations after.
i know how except rule part, no idea how check letter , how replace right number. know there's way like
if(string[x] == 'a-z')
but i'm neither sure how works, or how pick right number knowing it's letter.
there many approaches, more portable others.
the following looks thestring[x]
in array. if successful, pointer difference between , start value.
const char *alphanum = "0123456789abcdefghijklmnopqrstuvwxyz"; char *p = strchr(alphanum, string[x]); if (p == null || *p == '\0') { ; // not digit or a-z } else { int value = p - alphanum; // value; }
i'll leave handling of a-z
op. (convert upper or use array)
if code assumes ascii, a z
in sequential order:
int tovalue(char ch) { if (ch >= 'a' && ch <= 'z') return ch - 'a' + 10; if (ch >= 'a' && ch <= 'z') return ch - 'a' + 10; if (ch >= '0' && ch <= '9') return ch - '0'; // no match return -1; }
Comments
Post a Comment