Skip to main content
Code Review

Return to Question

edited tags; edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

In Place-place reverse of C-Style Stringstyle string

Source Link
coder
  • 2.5k
  • 4
  • 33
  • 60

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

AltStyle によって変換されたページ (->オリジナル) /