From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 18_reverse_str/.gitignore | 1 + 18_reverse_str/Makefile | 2 ++ 18_reverse_str/README | 15 +++++++++++++++ 18_reverse_str/grade.txt | 18 ++++++++++++++++++ 18_reverse_str/reverse.c | 39 +++++++++++++++++++++++++++++++++++++++ 18_reverse_str/reverse_ans.txt | 7 +++++++ 6 files changed, 82 insertions(+) create mode 100644 18_reverse_str/.gitignore create mode 100644 18_reverse_str/Makefile create mode 100644 18_reverse_str/README create mode 100644 18_reverse_str/grade.txt create mode 100644 18_reverse_str/reverse.c create mode 100644 18_reverse_str/reverse_ans.txt (limited to '18_reverse_str') 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 +#include +#include + +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 -- cgit v1.2.3