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[] = "This is my long string with lots of character.";
inPlaceReverseString(mylongstr, 7); // should reverse "This is"
cout << '"' << mylongstr << '"';
cout << " has " << strlen(mylongstr) << " characters\n";
...
It would print,
"i sihT" 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, "write code to reverse a C-Style String", not "write code to reverse a specified number of characters in a C-Style String". 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 < j; ++i, --j) {
std::swap(str[i], str[j]);
}
}
Note also that I slightly changed the loop-terminating logic, by checking i < 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 < num) {
// length of 'str' is less than 'num'. How to handle? ...
}
for (int i = 0, int j = strlen(str) - 1; i < 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[] = "This is my long string with lots of character.";
inPlaceReverseString(mylongstr, 7); // should reverse "This is"
cout << '"' << mylongstr << '"';
cout << " has " << strlen(mylongstr) << " characters\n";
...
would print:
"si sihT my long string with lots of character." has 47 characters
std::reverse(str, std::strchr(str, '\0'));
\$\endgroup\$