2

I would like to know how to "subtract letters" in C:
I mean, I have 2 letters, 'a' and 'c' and i want to execute 'c'-'a'='b' which is 3-1=2.
How is it possible to obtain the same behaviour in C?
I can conversion Letters->Numbers but how to manage the limited lenght of the alphabet? Thank you.

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
JJJ
  • 43
  • 1
  • 1
  • 3
  • Thanks for the answers, what I needed exactly was managing the fact that alphabet has 26 letters. I mean, doing as you suggest I couldn't do b-z or b-d for example. – JJJ May 28 '12 at 03:11
  • 2
    What do you expect the result to be when calculating `'b'-'z'`? – Alex Lockwood May 28 '12 at 03:19
  • 'b' - 'z' == 'c', twain249 perfectly understood, thank you. – JJJ May 28 '12 at 11:14

2 Answers2

7

you can treat the letters as numbers and then add the letter 'a' back to normalize it

so

char c1 = 'a';
char c2 = 'c';
int diff = c2 - c1; //'c' - 'a' = 2
char ans = diff + 'a' - 1; //add 'a' and subtract 1 to normalize it

If you want the number difference just use diff from my answer (ans will give you the letter).

This will not wrap around so

'a' - 'b' 

will result in -1 (or the character before a)

If you want to handle negatives with a wrap you have to check it

int diff = c2 - c1;
char ans;
diff > 0 ? ans = diff + 'a' - 1 : 'z' + diff + 1; 

This will give:

  1. 'z' for 'b'-'c'
  2. 'y' for 'b'-'d'
twain249
  • 5,666
  • 1
  • 21
  • 26
0

Are you looking this?

char a1,a2,a3;
a3 = a1 - a2 + 'a';
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
RolandXu
  • 3,566
  • 2
  • 17
  • 23
  • Thanks for the answer, what I needed exactly was managing the fact that alphabet has 26 letters. I mean, doing as you suggest I couldn't do b-z or b-d for example. – JJJ May 28 '12 at 03:11
  • in C language. the 'a' ~ 'z' is represented to number, so you can do it like the integer. so the b - d = -2. – RolandXu May 28 '12 at 03:16
  • Just have in mind that the signedness of the char type is implementation-defined. This code is non-portable and will possibly cause undefined behavior because of integer underflow. For example `a3 = 'a' - 'b' + 'a'` is undefined behavior on implementations where the char type is unsigned. – Lundin May 28 '12 at 06:30
  • @Lundin The char is implementation-defined but it is not to say the calculation of char is implementation-defined. First, the compiler promotes the char to int to calculate the expression. Second, in C99 standard 6.2.5, it said "If a member of the basic execution character set is stored in a char object, its value is guaranteed to be positive." – RolandXu May 28 '12 at 14:49
  • @RolandXu The integer -1 is not a member of any known character set, so there are no guarantees. The main problem here is that the characters are promoted to int, which is guaranteed to be signed. But then the result is stuffed back into a char again. Had the result been stored in an int and not a char, the code would be safe. – Lundin May 28 '12 at 14:57
  • @Lundin Sure, if the value cross the range of character, the result is undefined. But value, int type, of expression is well defined. if the value map in the range of character, the result is also well defined. – RolandXu May 28 '12 at 15:03