0

I'm trying to reverse an array of characters and place it in a new array. It doesn't seem to be returning anything. Here is the code below, I can't figure out waht is wrong. Any pointers? (No pun intended)

void getReverse(char dest[], char src[])
{
    int i;
    int j=0; 
    int length = strlen(src);
    printf("%d\n", length);

    for(i=length-1; i>=0; i--, j++)
        //for(j=0; j<length; j++)
        {
            dest[j]=src[i];
            putchar(j);
            j++;
        }
        dest[j] = '\0';
}

main()
{
    char dest[MAX_SIZE];
    char src[MAX_SIZE];
    int i = 0;
    int count;
    int c = getchar();

    count = 0;

    while ((count < MAX_SIZE) || (c != EOF))
    {
        src[count] = c;
        ++count;
        //putchar(c);
        c = getchar();  

    }

    getReverse(dest, src);
    printf("%s", dest);     
}
2
  • 7
    remove j++ from inside the loop, you have it in for Commented May 23, 2013 at 0:29
  • 1
    You have not guaranteed that src[] ends with a zero-terminator, so your reverse function's call to strlen() will yield an unpredictable result. Also I would rename getReverse() to reverse() or similar because the function does not 'get' anything (it does not return a value). Commented May 23, 2013 at 0:53

4 Answers 4

2

bugs:

  1. as @Bill said, j++ execute in the loop of getReverse() twice.

  2. the putchar(j) in getReverse() will lead to a confused output, as j is very little they may be unprintable character. If you really want to monitor the value of j, use printf("%d\n",j);

  3. as @jarmod said ,you miss a '\0' in src[]. You should add "src[count] = '\0';" after the loop of while() and before getReverse() in main().

in addition,your getReverse() can't work if *src and *dest point to the same address.Althogh you have declared that the function "reverse an array of characters and place it in a new array", but consider this when you write a more common function.

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

Comments

1

I just modify some code and run it. the code as follow

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

#define MAX_SIZE 30

void getReverse(char dest[], char src[])
{
    int i;
    int j=0;
    int length = strlen(src);
    printf("%d\n", length);

    for(i=length-1; i>=0; i--, j++)
    //for(j=0; j<length; j++)
    {
        dest[j]=src[i];
        //putchar(j);
        //j++;
    }
    dest[j] = '\0';
}

main()
{
    char dest[MAX_SIZE];
    char src[MAX_SIZE];
    int i = 0;
    int count;
    int c = getchar();

    count = 0;

    memset(src, 0, MAX_SIZE);

    while ((count < MAX_SIZE - 1) && c != EOF)
    {
        src[count] = c;
        ++count;
        //putchar(c);
        c = getchar();  

    }

    printf("src=%s\n", src);

    getReverse(dest, src);
    printf("dest=%s\n", dest);     
}

the result as follow

abcdefghijk
src=abcdefghijk
11
dest=kjihgfedcba

Comments

0

Here the value of j incremented twice.

for(i=length-1; i>=0; i--, **j++**)
        //for(j=0; j<length; j++)
        {
            dest[j]=src[i];
            putchar(j);
            **j++**;
        }
        dest[j] = '\0';
}

This may be the problem.

Comments

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

void getReverse(char dest[], char src[])
{
    int i;
    int j=0; 
    int length = strlen(src);
    printf("%d\n", length);

    for(i=length-1; i>=0; i--, j++)
        //for(j=0; j<length; j++)
        {
            dest[j]=src[i];
            //putchar(dest[j]);
            //j++;//duplicate , `for` after expression
        }
        dest[j] = '\0';
}

#define MAX_SIZE 32

int main(void){
    char dest[MAX_SIZE];
    char src[MAX_SIZE];
    //int i = 0;//unused
    int count;
    int c = getchar();

    count = 0;

    while ((count < MAX_SIZE) && (c != EOF))//|| -> &&
        if(c == '\n'){
            src[count] = '\0';
            break;
        }
        src[count] = c;
        ++count;
        //putchar(c);
        c = getchar();  

    }

    getReverse(dest, src);
    printf("%s", dest);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.