0

I'm trying to print a reversed string/array. I've used the following code and it seems to be able to give my second array, revString, values in the correct order from the first array string. I am also able to print individual characters in both arrays, and I'm able to print the entire string of the first array. However the revString array doesn't print at all. I'm wondering if I am missing a huge point here.

void reverseString(char string[]) {
    int i = strlen(string);
    int i_2 = 0;
    revString arrayet
    char revString[i + 1];
    char *character; 

    while (i > -1) {
        character = &string[i];
        revString[i_2] = *character;
        printf("%c", revString[i_2]);
        i = i - 1;
        i_2 = i_2 + 1;
    }
    revString[i_2] = '\0';
    printf("%d\n", i_2);
    printf("%s", revString);
}

The code gives now the following output with example string "Hello World";

dlrow olleH13

As you can see the final printf statement doesn't do anything

9
  • I think it prints the value, but your program quits before the value shows up to the console. Add \n after %s to see the printout. Commented Sep 9, 2018 at 10:52
  • You want to double check indexing of the source string. Which elements of the source array do access during the 1st iteration? Commented Sep 9, 2018 at 13:51
  • Also you might like to read this: How to debug small programs Commented Sep 9, 2018 at 13:56
  • character = &string[i]; revString[i_2] = *character; is a very roundabout way to say revString[i_2] = string[i];. Commented Sep 9, 2018 at 14:04
  • the final printf statement doesn't do anything because the first character of the reversed string is ____ (fill in the blank; use your debugger if necessary). Commented Sep 9, 2018 at 14:06

2 Answers 2

2

In C language indexing is 0 based. so, if you make a string of 10 length, the last character will be at index 9.

In your code, when you are assigning characters to revString, your code is trying to access string[len].

your code should be like this..

int i = strlen(string) - 1;

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

Comments

1

Your code reverses the string string including the null terminator at string[i]. The resulting array starts with a null terminator, hence printf outputs nothing.

Here is a modified version:

void reverseString(char string[]) {
    int i = strlen(string);
    int i_2 = 0;
    char revString[i + 1];
    char character;

    while (i > 0) {
        i = i - 1;
        character = string[i];
        revString[i_2] = character;
        //printf("%c", revString[i_2]);
        i_2 = i_2 + 1;
    }
    revString[i_2] = '\0';
    printf("%d\n", i_2);
    printf("%s", revString);
}

Output:

11
dlrow olleH

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.