-3

Beginner programmer here. I'm trying to take an input from user, reverse it and show the result. For some reason, it's printing blanks instead of the reversed string. I know that array[i] has the right information because if I use this loop on line for (int i=0; i<count; i++), it's printing the right characters. It's just not printing in reverse. What am I not getting here?

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

int main(void)
{
    printf("Please enter a word: ");
    char *word = get_string();

    int count = strlen(word);

    char array[count];

    for (int i=0; i< count; i++)
    {
        array[i] = word[i];
    }

    for (int i=count-1; i==0; i--)
    {
        printf("%c ", array[i]);
    }
    printf("\n");
}
5
  • 3
    Your condition is wrong. It should be: for (int i=count-1; i>=0; i--) Commented Feb 23, 2017 at 20:22
  • What's the point of copying the input verbatim someplace else? Why not just print it from where it is? Commented Feb 23, 2017 at 20:23
  • If you want to reverse the string your first for loop has to change. One array counts up the other counts down Commented Feb 23, 2017 at 20:26
  • @P.P. thank you, I don't know why it didn't come to my mind. Commented Feb 23, 2017 at 20:41
  • @DavidSchwartz I did all in one array at first, broke it down to debug it better. Commented Feb 23, 2017 at 20:42

2 Answers 2

1
for (int i=0; i< count; i++)
{
    array[i] = word[i];
}

You go over the string and copy it, you do not reverse it.

There is also a subtle bug in-waiting in your declaration of array, since you do not leave space for the '\0' character terminator. Passing your buffer to printf as a C-string, as opposed to character by character will have undefined behavior.

So to fix those two particular errors:

char array[count + 1];
array[count] = '\0';

for (int i = 0; i< count; i++)
{
    array[i] = word[count - i];
}

As a side note, it may not mean much to use a VLA for this small exercise, but for larger inputs it could very well overflow the call stack. Beware.

Sign up to request clarification or add additional context in comments.

9 Comments

and you forgot the null termination character (and the buffer is too short by 1)
@Jean-FrançoisFabre - did I? If this is why you down-voted, you really are trigger happy
@Jean-FrançoisFabre He's not using a termination character and his buffer is not too short. The buffer holds characters whose length is known, not a C-style string. There is no need to allocate an extra byte.
@Jean-FrançoisFabre - Easy enough mistake to make, a downvote followed by a comment from you not 15 seconds later. Apologies.
@Jean-FrançoisFabre - It's a bit of a chicken and egg thing. I usually improve in iterations until I'm satisfied. But I can't prove it's not the DV that made me do it. And regardless, the fact the DV remains strongly suggests it's not a factual DV.
|
0
// the header where strlen is
#include <string.h>

/**
 * \brief reverse the string pointed by str
**/
void reverseString(char* str) {
    int len = strlen(str);
    // the pointer for the left and right character
    char* pl = str;
    char* pr = str+len-1;
    // iterate to the middle of the string from left and right (len>>1 == len/2)
    for(int i = len>>1; i; --i, ++pl, --pr) {
        // swap the left and right character
        char l = *pl;
        *pl = *pr;
        *pr = l;
    };
};

And just call the function:

int main(void) {
    printf("Please enter a word: ");
    char *word = get_string();

    // Just call the function. Note: the memory is changed, if you want to have the original and the reversed just use a buffer and copy it with srcpy before the call
    reverseString(word)
    printf("%s\n", word);
};

And just change

char array[count];

for (int i=0; i< count; i++)
{
    array[i] = word[i];
}

to

// add an other byte for the null-terminating character!!!
char array[count+1];
strcpy(array, word);

2 Comments

code without any explanation isn't gonna help a bit. You should post assembly code while you're at it.
okay but there are tons of code snippets which do that on SO (follow my duplicate link). The OP wants to know the problems in his code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.