1

I want to print a string backwards. But my code seems to count down the alphabet from the last letter in the array to the first letter in the array instead of counting down the array itself and spitting out each letter in the array.

My code,

   #include <stdio.h>
   #include <string.h>

   int main(void) {

char word[50];
char end;
char x;

printf("Enter a word and I'll give it to you backwards: ");

scanf("%s", word);

end = strlen(word) - 1;

for (x = word[end]; x >= word[0]; x--) {
    printf("%c", x);
}

return 0;
}

Any suggestions? Thank you.

Deepu
  • 7,592
  • 4
  • 25
  • 47
Corey
  • 63
  • 1
  • 2
  • 8
  • your error is that your not getting the element in your array, you get the value of your last character (examples the letter u) then you decrement the value of that letter and print that (u, t, s, r...) that explains your reverse alphabet – Jason Rogers Dec 02 '10 at 03:32
  • I was curious so I posted in a different question http://stackoverflow.com/questions/4331486/loops-in-c-challenge-can-it-be-done-another-way/4331501#4331501. and there is a "different" way to loop through the array that doesn't involve a counter. – Jason Rogers Dec 02 '10 at 04:00
  • Since you're here... please be sure to read up on the problems with scanf before using it in real code: http://stackoverflow.com/questions/2430303/disadvantages-of-scanf – Nicholas Knight Dec 02 '10 at 04:43

6 Answers6

7

What you have loops between the array element values. You want to loop between the array indexes. Update your loop to the following:

for (x = end; x >= 0; --x) {
    printf("%c", word[x]);
}

Note that this goes from the last index to zero and output the character at that index. Also a micro-optimization in the for loop using pre-decrement.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • What is the point of the micro optimization here ? it isn't changing the evaluation of the for or the print? (I'm not criticizing, I just don't understand the difference of pre vs post increment in the case) – Jason Rogers Dec 02 '10 at 03:34
  • Thanks Jason (McCreary)! I feel stupid being that close and not working through it! *face palm* -- could you elaborate on Jason's (Rogers) comment, I too do not see the difference in this case with using a pre-decrement. I changed the code and tested and saw no difference, does it just run through the loop an extra time unnecessarily? – Corey Dec 02 '10 at 03:39
  • 1
    @Jason, this has to do with how the pre and post decrement operators compile. Pre compiles to decrement, store. Post compiles to read, decrement, store. Note the extra *read* step. Again, a **micro** optimization. But one that can be commonly used in loops as there is rarely an explicit need to *read* the value first. However, there may be a need in a statement like `b = 1 + x--;`. – Jason McCreary Dec 02 '10 at 03:43
  • 1
    oooh ok. I know the difference in post and pre I just didn't see the point here, but I try to make it a point to understand the differences in codes (even if micro). FYI to all: the exact difference in between post and pre increment is a favorite question of recruiters ^^ – Jason Rogers Dec 02 '10 at 03:52
  • @Jason, indeed. But I consider my point a bit deeper. On the surface the difference is order of operation. pre updates the value before the rest of the expression, post does after. `b = 1 + x--;` and `b = 1 + --x;` will give you different results. – Jason McCreary Dec 02 '10 at 04:00
  • yep I know, actually you can see that in the debugger, the debugger seems to go one extra step on the for statement. – Jason Rogers Dec 02 '10 at 04:02
  • @JasonM So in your first example x will be decremented after 1 is added and the second example x will be decremented before 1 is added? – Corey Dec 02 '10 at 04:07
  • @Corey, exactly. That's the difference in the operators. Because of which, pre is best to use in situations where it's not part of a larger evaluated expression. Again, a **micro** optimization. – Jason McCreary Dec 02 '10 at 14:05
4

You're calling array values and not the specific index.

for(x = end; x >= 0; x--) { printf("%c", word[x]); }
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

You want to print word[x] (the xth character in the array) instead of x (the xth character in the character set).

You also want to be counting down indexes, not characters.

for(x=end, x >= 0; x--)
    printf("%c", word[x]);
Anon.
  • 58,739
  • 8
  • 81
  • 86
0
//Change end to int type and modify your for loop as shown.
#include <stdio.h>
#include <string.h>

int main(void) 
{

char word[50];
int end;
char x;

printf("Enter a word and I'll give it to you backwards: ");

scanf("%s", word);

end = strlen(word) - 1;

 for (x = end; x >= 0; x--) 
 printf("%c",word[x] );


return 0;
}
venom
  • 1
0

In your loop, x is the index into the character array comprising word. So x should change from end to 0, and referencing the array should be as word[x].

ysap
  • 7,723
  • 7
  • 59
  • 122
0
#include <stdio.h>
#include <stdlib.h>

/*
 * 
 */
int main(int argc, char** argv) {
    int i;
    char letters[3]="";
    printf("Enter three letters!");
    scanf("%s",letters);
    for(i=3;i>=0;i--){
        printf("%c", letters[i]);
    }
    return (EXIT_SUCCESS);
}