Take the 2-minute tour ×

I'm trying to solve the problem here but I don't know why my code isn't working. Any help is appreciated. EDIT: Edited to make correction mentioned below, but there is still an extra "15" (in bold) on the second line of the output and I don't understand where it is coming from.

My output is

18662658515 555227215

#include <stdlib.h>
#include <stdio.h>

int main(void){
  int n;
  int j;
  int i = 0;
  char mystr[15];


        printf("%c", mystr[j]);
      if ('A' <= mystr[j] && mystr[j] <= 'C')
      if ('D' <= mystr[j] && mystr[j] <= 'F')
      if ('G' <= mystr[j] && mystr[j] <= 'I')
      if ('J' <= mystr[j] && mystr[j] <= 'L')
      if ('M' <= mystr[j] && mystr[j] <= 'O')
      if ('P' <= mystr[j] && mystr[j] <= 'S')
      if ('T' <= mystr[j] && mystr[j] <= 'V')
      if ('W' <= mystr[j] && mystr[j] <= 'Z')
share|improve this question
perhaps you could elucidate what is going wrong. –  John Nolan May 4 '09 at 21:40
The output of my program doesn't match the output of the sample run. –  Stu May 4 '09 at 21:42

5 Answers 5

up vote 3 down vote accepted

The problem is that you're iterating over all 15 characters in the input string, regardless of the length of the input. The first test case has 11 characters, but the second case has only 8. In the second iteration, you're accidentally processing the last two characters from the first input, which were 15.

To fix it, just stop your iteration when you hit the NUL character 0, which terminates the string by changing this line



for(j=0; mystr[j] != 0; j++){
share|improve this answer
Thanks. I can see why people don't like writing in C :( –  Stu May 4 '09 at 22:11

For one thing, I think your comparisons are backwards. For example, you should be testing "if ('A' <= mystr[j] && mystr[j] <= 'C')".

share|improve this answer
Yup; if you're going to use the backwards comparison notation, you need to use the consistent notation. –  Jonathan Leffler May 4 '09 at 21:49

It would be helpful, perhaps, to have a function between() (with apologies; my C is rusty):

bool between(char c, char before, char after) {
    return before <= c && c <= after;




if (between(mystr[j], 'A', 'C')

It is generally better to use half-open ranges, where the lower limit is inclusive and the upper limit exclusive. With this, then, the last element of each test would be the first element of the preceding test, which could help you detect certain kinds of bugs more readily.

share|improve this answer

consider that you might want to compare things in the same order. Speak this out in plain english before writing code. If myLetterCode is Greater Than A.code && myLetterCode is Less than C.Code (it must be B!).

keep your letter on the left and the thing you're comparing it to on the right. Otherwise it gets very confusing very fast.

share|improve this answer

Wouldn't this be easier with a lookup table?

int numbers[] = {              2, 2, 2,  3, 3, 3,

... cut ...

if (isdigit (mystr[j]))
    printf ("%d", numbers[mystr[j] - 'A']);

... cut ...
share|improve this answer
That is a cleaner way but I was not clever enough to think of it. –  Stu May 4 '09 at 22:19

Your Answer


