From f38ae6d885eeaeb368b119d110c39080be18a5c4 Mon Sep 17 00:00:00 2001 From: Olivierlu1 <180256@hkis.edu.hk> Date: Mon, 11 Jul 2016 00:13:16 -0400 Subject: [PATCH] Assignment 7 --- assignment7.txt | 18 ++++++++++++++++++ reverse.c | 18 ++++++++++++++++++ stringf.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 assignment7.txt create mode 100644 reverse.c create mode 100644 stringf.c diff --git a/assignment7.txt b/assignment7.txt new file mode 100644 index 0000000..4397543 --- /dev/null +++ b/assignment7.txt @@ -0,0 +1,18 @@ +Olivier Gabison + +1. ++*p dereferences the value that the p variable is pointing to and then increments it. While *p++ increments the pointer, +and then dereferences it. Finally, *++p first increments the pointer and then dereferences it. + +2. No, order of precedence is not done through left to right order. + +3. I think the best uses of pointers is passing by reference and dynamic starting sizes on arrays. + +4.1 char *, its a string, and char[] has '\0' at the end of it. +4.3 1 - the ASCII value of '\0' is 0. Therefore, 0 = 0, which returns 1; +4.4 int, dereference of pointer a +4.5 int *, it creates a pointer that is used upon a[0] +4.6 int, dereference of pointer p +4.7 int **, creates a pointer for pointer p +4.8 char *, derefernce of the double pointer 'argv' +4.9 This does not work as you cannot cast a pointer to a function +4.10 5 (only when string.h is included) diff --git a/reverse.c b/reverse.c new file mode 100644 index 0000000..5cbaf40 --- /dev/null +++ b/reverse.c @@ -0,0 +1,18 @@ +#include + +int main(){ + + char word[100] = "HELLOHH"; //THIS IS THE WORD THAT WILL BE REVERSED + char *Start = word - 1; + char *End = Start + strlen(word); + int i; + for (i=0; *Start!=*End; i++) { + printf("%c",*End); + End--; + } + printf("\n"); + + + + return 0; +} diff --git a/stringf.c b/stringf.c new file mode 100644 index 0000000..6164360 --- /dev/null +++ b/stringf.c @@ -0,0 +1,48 @@ +#include + +int strcmp(char *a, char *b){ + if(*a > *b){ + return 1; + } else { + if (*a < *b){ + return -1; + } else { + a++; + b++; + return strcmp(a, b); + } + } +} + +char *strcat(char *a, char *b){ + char *answer[100]; + int i; + + for(i = 0; i < 100; i++){ + if(*a == '\0'){ + break; + } + answer[i] = *a; + a++; + } + + for (int j = i; i < 100; j++){ + if(*b == '\0'){ + break; + } + answer[j] = *b; + b++; + } + + return answer; +} + +int main(){ + + char s1[] = "Hello"; + char s2[] = "Hallo"; + + printf("%s", strcat(s1,s2)); + + return 0; +}