In Place-place reverse of C-Style Stringstyle string
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 <iostream>
void inPlaceReverseString(char str[], int size)
{
int j = size - 2;
int i;
for (i = 0; i < (size-1)/2; ++i)
{
std::swap(str[i], str[j]);
j--;
}
str[size - 1] = '0円';
}
int main()
{
char str[255];
std::cout << "Enter String\n";
std::cin.getline(str, 255);
std::cout << "String: " << str << '\n';
int size = 0;
while (str[size] != '0円')
{
size++;
}
size++;
inPlaceReverseString(str, size);
std::cout << "Reversed String: " << str << '\n';
}
lang-cpp