Reversing a string in C without the output being null

Julie Thompson

I am trying to reverse a string (character array) using the following code, but when I attempt to print the string, the value of null. This is a homework assignment, but I am trying to learn so any help would be appreciated.

void input_reverse_string(const char* inputStr, char* reverseStr)
{
    int i = 0;
    int length = 0;

    for (; *(inputStr++) != '\0'; i++)
    {
        length++;
    }

        while (*inputStr)
    {
        *reverseStr = *inputStr;
        inputStr++;
        reverseStr++;
    }

    
    const char* chr_ptr = &inputStr[length - 1];
    printf("I see a %s\n", *chr_ptr);


    *reverseStr = '\0';
    
    printf("%d", length);

    /* return reverseStr; */ 
} 
Marco Bonelli

Several things are out of order:

  1. That's a strange way of computing the length of a string. You are using an index variable that you don't need, and incrementing 3 things at the same time, it's unneeded to say the least.

  2. After calculating the length, and incrementing the inputStr pointer up to its end, you don't reset the pointer, so it still points to the end of the string (actually, one after the end!).

  3. Inside the while you are advancing both pointers (inputStr and reverseStr) in the same direction, which can't possibly be right if you want to reverse the string.

The correct way to do this would be:

  1. Compute the length of the string. Either use strlen() or do it by hand, but you really only need to increment one variable to do this. You can avoid incrementing inputStr, just use a temporary pointer.

  2. Start from inputStr + length and walk backwards. Either use a pointer and do -- or just index the string).

Here's a working example:

void reverse_string(const char* inputStr, char* reverseStr) {
    unsigned len = 0;
    int i;

    while (inputStr[len])
        len++;

    for (i = len - 1; i >= 0; i--) {
        reverseStr[len - i - 1] = inputStr[i];
    }

    reverseStr[len] = '\0';
}

int main(void) {
    char a[6] = "hello";
    char b[6];

    reverse_string(a, b);
    puts(b);

    return 0;
}

Output:

olleh

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reversing a string in C without the output being null

From Dev

Reversing a string in c++

From Dev

Reversing a string in c++

From Dev

reversing a string in c with pointer

From Dev

Reversing a string without using pointers

From Dev

String is not being directed to Output file (C++)

From Dev

C: Reversing a string - pointer confusion

From Dev

Reversing a string in C using Recursion

From Dev

Reversing a string in place in C - pointers?

From Dev

JAVA Reversing a string on multiple lines output

From Dev

Reversing a string - output skips last character

From Dev

Reversing string without affecting any special characters

From Dev

Reversing a string using recursion without <algorithm> or loops

From Dev

C++ output string without '\n' or endl will output '#' in the end of string

From Dev

Reversing a string in C w/o using `reverseStr()`

From Dev

Reversing a '\0' terminated C string in place?

From Dev

Time Complexity in Reversing a C++ String

From Dev

Reversing a '\0' terminated C string in place?

From Dev

(C++) Reversing a string using stacks?

From Dev

Reversing a string in C w/o using `reverseStr()`

From Dev

Reversing words in a string JS without using built in function

From Dev

reversing input - undefined output

From Dev

Reversing numbers in C without using built in reverse function

From Dev

Reversing numbers in C without using built in reverse function

From Dev

My way of reversing a String using recursion in C language

From Dev

Reversing the whole String and reversing each word in it

From Dev

Reversing the order of a string

From Dev

Reversing a string array in java

From Dev

performing IP reversing in string

Related Related

HotTag

Archive