In-place reverse of C-style string This is a question from the book "Cracking the Coding Interview".


  Write code to reverse a C-Style String (C-String means that “abcd” is
  represented as five characters, including the null character )


Please review this code.

#include &lt;iostream&gt;

void inPlaceReverseString(char str[], int size)
{  
    int j = size - 2;
    int i;

    for (i = 0; i &lt; (size-1)/2; ++i)
    {
        std::swap(str[i], str[j]);
        j--;
    }
    str[size - 1] = '\0';
}

int main()
{
    char str[255];
    std::cout &lt;&lt; "Enter String\n";
    std::cin.getline(str, 255);
    std::cout &lt;&lt; "String: " &lt;&lt; str &lt;&lt; '\n';
    int size = 0;
    while (str[size] != '\0')
    {
        size++;
    }
    size++;
    inPlaceReverseString(str, size);
    std::cout &lt;&lt; "Reversed String: " &lt;&lt; str &lt;&lt; '\n';
}
Because this is an attempt to answer an interview question, let's assume a hypothetical interviewer then asked you,

what would happen if I called your function using the following code:
...
char mylongstr[] = &quot;This is my long string with lots of character.&quot;;
inPlaceReverseString(mylongstr, 7); // should reverse &quot;This is&quot;
cout &lt;&lt; '&quot;' &lt;&lt; mylongstr &lt;&lt; '&quot;';
cout &lt;&lt; &quot; has &quot; &lt;&lt; strlen(mylongstr) &lt;&lt; &quot; characters\n&quot;;
...


It would print,
&quot;i sihT&quot; has 6 characters

That doesn't seem to make sense. Not only that, your function potentially clobbered the rest of my string, by writing \0 over the s in the word is.
Don't break expectations/convention with strings
@koblas mentioned to use the standard library function strlen().
strlen() returns the number of characters, not including the terminating \0 character, in the string passed to it. C and C++ string handling functions that take a count parameter generally expect that count refers to the number of characters to work on.
But the size parameter to inPlaceReverseString() breaks that expectation. I think you're overthinking the C-style string's terminating \0 character.
For interview questions (as well as coding in general), start with the simplest thing that can work and meets the specifications
Note that the interview question you quoted said, &quot;write code to reverse a C-Style String&quot;, not &quot;write code to reverse a specified number of characters in a C-Style String&quot;. For an interview-type question like this, rather than pass the length of the string as a parameter, just discover the length of the string within your function:
void inPlaceReverseString(char str[])
{  
    for (int i = 0, int j = strlen(str) - 1; i &lt; j; ++i, --j) {
        std::swap(str[i], str[j]);
    }
}

Note also that I slightly changed the loop-terminating logic, by checking i &lt; j. This makes the logic a bit easier to read, making it obvious that when the indexes i and j pass each other, there's no more replacement to be done.

Now, if you still want to have a length parameter (or are asked to extend the simple answer I wrote above), a couple suggestions:

Use a parameter name such as num, len, or even n, as opposed to size. This is really pedantic on my part, but size tends to suggest memory sizes, as opposed to length of strings or number of characters.

Use size_t as the type of the length parameter. This is an unsigned type, and conforms with the rest of C-style string functions that have a length/number parameter.

Don't write a terminating \0 after the in place reversal.

Even if a length is specified, you should still check for a terminating \0 character within the string.


So, a possible implementation of the answer would look something like:
void inPlaceReverseString(char str[], size_t num)
{
    int len = strlen(str);
    if (len &lt; num) {
        // length of 'str' is less than 'num'. How to handle? ...
    }
    for (int i = 0, int j = strlen(str) - 1; i &lt; j; ++i, --j) {
        std::swap(str[i], str[j]);
    }
}

Now, the original hypothetical question asked by the hypothetical interviewer at the start of this answer would work:
...
char mylongstr[] = &quot;This is my long string with lots of character.&quot;;
inPlaceReverseString(mylongstr, 7); // should reverse &quot;This is&quot;
cout &lt;&lt; '&quot;' &lt;&lt; mylongstr &lt;&lt; '&quot;';
cout &lt;&lt; &quot; has &quot; &lt;&lt; strlen(mylongstr) &lt;&lt; &quot; characters\n&quot;;
...

would print:
&quot;si sihT my long string with lots of character.&quot; has 47 characters