From d8cf59cd7560800bd05a11177fd5e466d6e791bf Mon Sep 17 00:00:00 2001 From: Skee Date: Thu, 7 Jul 2016 21:29:07 -0400 Subject: [PATCH 1/3] String Reversal Function --- reverse.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 reverse.c diff --git a/reverse.c b/reverse.c new file mode 100644 index 0000000..e7d3e17 --- /dev/null +++ b/reverse.c @@ -0,0 +1,31 @@ +/*Sean Kee*/ +/*Reverse inputted string using pointers*/ +#include +#include + +void stringReverse(char string[]) { + char swap; + char *beginptr = &string[0]; + char *endptr = &string[strlen(string) - 1]; + + while (beginptr < endptr) { + swap = *beginptr; + *beginptr = *endptr; + *endptr = swap; + beginptr++; + endptr--; + } + + printf("New String: %s\n", string); + +} + +int main() { + char input[100]; + printf("Input a string\n#: "); + fgets(input, sizeof(input), stdin); + input[strlen(input)-1] = '\0'; + stringReverse(input); + + return 0; +} From 150b937b7df7fa90efd733ce85699b8bba6225a6 Mon Sep 17 00:00:00 2001 From: Skee Date: Sun, 10 Jul 2016 22:29:25 -0400 Subject: [PATCH 2/3] String cat/comp functions --- strfxn.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 strfxn.c diff --git a/strfxn.c b/strfxn.c new file mode 100644 index 0000000..3634fb1 --- /dev/null +++ b/strfxn.c @@ -0,0 +1,62 @@ +/*Sean Kee*/ +/*String Functions*/ + +#include + +void stringcat(char *str1, char *str2) { + while (*str1 != '\n') { + str1++; + } + while (*str2 != '\n') { + *str1 = *str2; + str2++; + str1++; + } + *str1 = '\0'; + +} + +int stringcmp(char *str1, char *str2) { + while (*str1 != '\n' || *str2 != '\n') { + if (*str1 == *str2) { + str1++; + str2++; + } + else + return 1; + } + return 0; +} + +int main() { + int menu = 1; + int option; + char input1[100]; + char input2[100]; + int returned; + printf("Input String 1\n#: "); + fgets(input1, sizeof(input1), stdin); + printf("Input String 2\n#: "); + fgets(input2, sizeof(input2), stdin); + + while (menu == 1) { + printf("\nWhat would you like to do?\n1: Concatenate Strings\n2: Compare Strings\n3: Exit\n#: "); + scanf("%d", &option); + switch(option) { + case 1: + stringcat(input1, input2); + printf("\nNew String: %s\n", input1); + break; + case 2: + returned = stringcmp(&input1, &input2); + if (returned == 0) + printf("\nIdentical Strings\n"); + else + printf("\nNot Identical Strings\n"); + break; + case 3: + menu = 0; + break; + } + } +} From 3fcb110b72af99628336a1e5635d935a84f17a6c Mon Sep 17 00:00:00 2001 From: Skee Date: Sun, 10 Jul 2016 23:47:03 -0400 Subject: [PATCH 3/3] assignment 7 questions --- assignment7.txt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 assignment7.txt diff --git a/assignment7.txt b/assignment7.txt new file mode 100644 index 0000000..183ff3b --- /dev/null +++ b/assignment7.txt @@ -0,0 +1,18 @@ +Sean Kee + +1. For ++*p, it is read right to left. For *p++, it first adds, then does the *p. For *++p, it also reads right to left. + +2. Yes, left to right or right to left is guarenteed because the compiler uses a sort of "order of operations" in it's own sense. For terms that are the same in terms of precedence, the compiler reads right to left. + +3. The advantage of using pointers is that a) You can have a string with an undefined length. b) Pass variables by reference rather than value. + +4.1 char a[3] = "abc"; for a string +4.2 Invalid because you are trying to declare an array with a string, then using a - instead of a = +4.3 Invalid because you are trying to set a character from a string(end of string character) to an integer +4.4 Pointer- int *a = variable; Points to the variable address +4.5 Invalid because arrays already pass by refernce +4.6 Pointer- int *p = variable; Points to the variable address +4.7 Refernce- scanf("%d", &variable); You are giving the function the address of the variable so it can be used in a pointer. +4.8 *++argv; increases the value that argv is pointing to by 1 after the operation is complete. +4.9 Reference- fxn(&main); you are sending the address of the main variable to the function so that it can be used as a pointer. +4.10 sizeof(str); used to find the size of a string in terms of bytes.