Reputation: 47
i know there is isalpha(_)
function for the string.h but i'm just wondering if there is a function for us to know what letter is the specific char in a string. and i want to change it with another specific char such as all A will become "#--" and all K will become "--#" and all other letters has its own differences (also is this possible? to make a substring? inside a string? or am i doing it wrong..)
char string[100];
int i;
char A*[]={"#--","--#"};
gets(string);
for(i=0, i<100, i++) {
if(isalpha(string[i])) {
string[i]=A[1];
}
}
but i don't know a function that tells me what letter that is.. i know it's a letter.. i know what letter is it.. but the computer/compiler doesn't know what letter is it, it just know if its a letter or not..
Can we also use the strcmp(_)
? as an alternative? like if we make another char
that we just put the whole alphabet letters in order?
char alpha[26]={'a','b','c','d','e','f','g','h','i','j','k','l'*you get the point* };
char string[100];
int i;
char A*[]={"#--","--#"};
gets(string);
for(i=0, i<100, i++) {
if(strcmp(alpha,string[i])==0) {
string[i]=A[1];
}
}
or should i use the ASCII table? because we tested it out
printf("%d , %d \n", 'A', 'a');
printf("%c, %c \n", 65, 97);
this out puts:
65 , 97
A , a
what is the best move to do here?
Upvotes: 0
Views: 1133
Reputation: 4411
char c = 'A'
and char c = 65
is exactly the same thing.
If you want to compare 2 chars, just use the ==
operator:
char c = 'A';
char c2 = 65;
if (c == c2)
puts("It is equal!");
From this you can then create some functions/macro if you want to "abstract" the condition:
#define IS_LETTER(X, L) (X == L)
#define IS_LETTER_A(X) (IS_LETTER(X, 'A'))
char c = 'A';
if (IS_LETTER_A(c)) // or IS_LETTER(c, 'A')
puts("It's an A");
Upvotes: 2
Reputation: 8154
For this purpose, make yourself an array which contains the replacement characters. Then, access this array by the character you want to encode. Access is done by assuming that the char is an ASCII char, simply subtract the offset from the ASCII table (e.g. 65 if for letting it begin at A) and access your array. E.g.
char *code = [ '1', '2' ];
char input = 'B'; // 66 in ASCII
char output = code[input - 65];
Edit: of course you can do the same with e.g. char
coding to char*
(e.g. strings). Just use an array of strings instead of an array of characters.
Upvotes: 2