diff --git a/assignment7.txt b/assignment7.txt new file mode 100644 index 0000000..d3de44e --- /dev/null +++ b/assignment7.txt @@ -0,0 +1,16 @@ +1) + ++*p increments the value that p is pointing to + *p++ gets the value that p is pointing to, then increments p + *++p increments p, then returns the new value that p is pointing at +2) yes? +3) You can pass by reference +4.1) char * +4.2) 121 +4.3) 1 +4.4) 10 +4.5) int * +4.6) 12 +4.7) int ** +4.8) char * +4.9) invalid: we never learned about pointers to functions +4.10) 6 diff --git a/reverse.c b/reverse.c new file mode 100644 index 0000000..396edbd --- /dev/null +++ b/reverse.c @@ -0,0 +1,31 @@ +/* Harry Brickner + * Given a string, will print out the reversed string (minus the newline character at the end, if there is one.) */ +#include +#include + +void swap (char* a, char* b){ + char temp = *a; + *a = *b; + *b = temp; +} + +void reverse(char str[], int length){ + if(str[length - 1] == '\n') str[--length] = '\0'; + for(int i = 0; i < length / 2; i++){ + swap(&str[i], &str[length - i - 1]); + } +} + +void driver(){ + int length = 50; + char input[50]; + printf("Please input a string\n"); + fgets(input, length, stdin); + reverse(input, strlen(input)); + printf("Reversed string is:\"%s\"\n", input); +} + +int main(){ + driver(); + return 0; +}