diff --git a/assignment7.txt b/assignment7.txt new file mode 100644 index 0000000..b01f5bd --- /dev/null +++ b/assignment7.txt @@ -0,0 +1,5 @@ +Oscar So +1. First one means, ++(*p) adds one before implementing pointer. Second one is (*P++), but precedence of ++ is treated before P. Last one is similar to the second one, but it is treated as (*++P) where the compiler looks for associativity. +2. I think both do not guarentee operator precedence, this is because in postfix, &&, ||, mathematical functions, comparison, the computer code reads it from left to right. However, for conditions, assignment of variables, and prefixes, the code goes from right to left. +3. Advantages of pointers are when handling arrays, and data tables. They allow references to functions, and can return multiple variables and values. +4. I am a bit clueless on number 4 and would like some explanation. Thanks. diff --git a/reverse.c b/reverse.c new file mode 100644 index 0000000..da782af --- /dev/null +++ b/reverse.c @@ -0,0 +1,28 @@ +#include +#include +void reverse(char *input){ + int length, i; + char *start, *stop, temp; + length = strlen(input); + start=input; + stop=input; + for(i=0;i +#include +char *cat (char *p1, char *p2){ + int i=0, p3= strlen(p1); + while(p2[i]!='\0'){ + p1[p3]=p2[i]; + p3++; + i++; + } + p1[p3]='\0'; + return p1; +} + +int main(){ + char p1[50], p2[50]; + printf("Enter first string: "); + fgets(p1,sizeof(p1),stdin); + printf("Enter second string: "); + fgets(p2,sizeof(p2),stdin); + strtok(p1,"\n"); + strtok(p2,"\n"); + cat(p1,p2); + printf("New string: %s \n",p1); +} diff --git a/string2.c b/string2.c new file mode 100644 index 0000000..b54f0e9 --- /dev/null +++ b/string2.c @@ -0,0 +1,37 @@ +#include +#include +int compare(char *p1, char *p2){ + while (*p1==*p2){ + if (*p1 == '\0' || *p2 == '\0'){ + printf("Dissimilar User Input \n Error \n"); + break; + } + p1++; + p2++; + } + if (*p1=='\0' && *p2 == '\0'){ + return 0; + }else if (*p1=='\0'){ + return 1; + }else if (*p2 == '\0'){ + return -1; + } +} +int main () { + int result; + char p1 [100], p2 [100], final; + printf("Input first string: "); + fgets(p1, sizeof(p1),stdin); + printf("Input second string: "); + fgets(p2, sizeof(p2),stdin); + strtok(p2, "\n"); + strtok(p1, "\n"); + result = compare (p1,p2); + if (result == 0){ + printf("Both strings are the same. \n"); + }else if(result == -1) { + printf("Input 1 is longer than Input 2. \n"); + }else if (result ==1) { + printf("Input 1 is shorter than Input 2. \n"); + } +}