summaryrefslogtreecommitdiff
path: root/18_reverse_str
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
Great C programming funHEADmaster
Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git
Diffstat (limited to '18_reverse_str')
-rw-r--r--18_reverse_str/.gitignore1
-rw-r--r--18_reverse_str/Makefile2
-rw-r--r--18_reverse_str/README15
-rw-r--r--18_reverse_str/grade.txt18
-rw-r--r--18_reverse_str/reverse.c39
-rw-r--r--18_reverse_str/reverse_ans.txt7
6 files changed, 82 insertions, 0 deletions
diff --git a/18_reverse_str/.gitignore b/18_reverse_str/.gitignore
new file mode 100644
index 0000000..3ddc7c0
--- /dev/null
+++ b/18_reverse_str/.gitignore
@@ -0,0 +1 @@
+reverse
diff --git a/18_reverse_str/Makefile b/18_reverse_str/Makefile
new file mode 100644
index 0000000..784a04a
--- /dev/null
+++ b/18_reverse_str/Makefile
@@ -0,0 +1,2 @@
+reverse: reverse.c
+ gcc -o reverse -pedantic -std=gnu99 -Wall -Werror reverse.c
diff --git a/18_reverse_str/README b/18_reverse_str/README
new file mode 100644
index 0000000..af46aa6
--- /dev/null
+++ b/18_reverse_str/README
@@ -0,0 +1,15 @@
+For this problem, you will write the function
+
+ void reverse(char * str)
+
+in the provided reverse.c file. This function should reverse the string
+passed into it (that is, if given "abc", it should change the contents
+of that string to read "cba"). Note that this function's return type
+is "void"---it modifies the string passed into it in place.
+
+We have provided a main function which you can use to test your
+reverse function (do you recognize the quotations?). The correct
+output for your program can be found in reverse_ans.txt, so you can
+diff your program's output against it.
+
+Submit your reverse.c file for grading.
diff --git a/18_reverse_str/grade.txt b/18_reverse_str/grade.txt
new file mode 100644
index 0000000..594c87c
--- /dev/null
+++ b/18_reverse_str/grade.txt
@@ -0,0 +1,18 @@
+Grading at Sat 16 Oct 2021 01:39:49 AM UTC
+Attempting to compile reverse.c
+Your file matched the expected output
+Your output matched what we expected
+Removing your main() and replacing it with our own to run more tests...
+#################################################
+testcase2:
+nullptr#################################################
+testcase3:
+Your file matched the expected output
+#################################################
+testcase4:
+Your file matched the expected output
+#################################################
+testcase5:
+Your file matched the expected output
+
+Overall Grade: A
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;
+}
diff --git a/18_reverse_str/reverse_ans.txt b/18_reverse_str/reverse_ans.txt
new file mode 100644
index 0000000..7b70ecc
--- /dev/null
+++ b/18_reverse_str/reverse_ans.txt
@@ -0,0 +1,7 @@
+
+321
+dcba
+7.32524 etadratS ,gol s'niatpaC
+.ayotnoM oginI si eman ym ,olleH
+!yadyna namgniw ym eb nac uoY
+!worromot on eb yam erehT !secrof ruoy fo rewop lluf eht hsaelnU !sidneleS rotucexE