From ce533bf0e07573bba1d16f8515649426fbe56201 Mon Sep 17 00:00:00 2001 From: Michael Duarte Date: Mon, 14 Mar 2016 20:42:15 -0700 Subject: [PATCH] Avoid manually doing a loop to go to the end of the string. even if strlen has a complexity of O(n), the code looks cleaner. --- c++/Chapter 1/Question1_2/Question1_2.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/c++/Chapter 1/Question1_2/Question1_2.cpp b/c++/Chapter 1/Question1_2/Question1_2.cpp index bd2941ef..6a70d529 100644 --- a/c++/Chapter 1/Question1_2/Question1_2.cpp +++ b/c++/Chapter 1/Question1_2/Question1_2.cpp @@ -6,17 +6,10 @@ using std::endl; void Question1_2::reverse(char* str) { - char *ptrEnd = str; - char temp; - if (str) { - while (*ptrEnd) - { - ptrEnd++; - } - ptrEnd--; - + char *ptrEnd = &str[strlen(str) - 1]; + char temp; while (str < ptrEnd) { temp = *str; @@ -38,4 +31,4 @@ int Question1_2::run() } return 0; -} \ No newline at end of file +}