summaryrefslogtreecommitdiff
path: root/18_reverse_str/reverse.c
diff options
context:
space:
mode:
authorHaidong Ji2022-04-15 15:51:30 -0500
committerHaidong Ji2022-04-15 15:51:30 -0500
commit442a49ad5a48d417345959b903ae6a6d32d55759 (patch)
treec7127bb497e5e439018b1915e0136eec2c9cb124 /18_reverse_str/reverse.c
Great C programming funHEADmaster
Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git
Diffstat (limited to '18_reverse_str/reverse.c')
-rw-r--r--18_reverse_str/reverse.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/18_reverse_str/reverse.c b/18_reverse_str/reverse.c
new file mode 100644
index 0000000..bea6e4e
--- /dev/null
+++ b/18_reverse_str/reverse.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void reverse(char * str) {
+ if (!str) {
+ return;
+ }
+ size_t string_length = 0;
+ char temp;
+ const char * ptr = str;
+ // Find string length
+ while (*ptr != '\0') {
+ string_length++;
+ ptr++;
+ }
+
+ for (size_t i = 0; i < (string_length/2); i++) {
+ temp = str[i];
+ str[i] = str[string_length-1-i];
+ str[string_length-1-i] = temp;
+ }
+}
+
+int main(void) {
+ char str0[] = "";
+ char str1[] = "123";
+ char str2[] = "abcd";
+ char str3[] = "Captain's log, Stardate 42523.7";
+ char str4[] = "Hello, my name is Inigo Montoya.";
+ char str5[] = "You can be my wingman anyday!";
+ char str6[] = "Executor Selendis! Unleash the full power of your forces! There may be no tomorrow!";
+ char * array[] = {str0, str1, str2, str3, str4, str5, str6};
+ for (int i = 0; i < 7; i++) {
+ reverse(array[i]);
+ printf("%s\n", array[i]);
+ }
+ return EXIT_SUCCESS;
+}