diff --git a/assignment7.txt b/assignment7.txt new file mode 100644 index 0000000..cdbda49 --- /dev/null +++ b/assignment7.txt @@ -0,0 +1,14 @@ +Jack Rosen +1. ++*p increments the value at point p. *p++ increments p and then returns the value. *++p increments p and then returns the value as well. +2. It is only guaranteed for certain operators. The first level of operators goes from left to right, the second goes from right to left, and the rest go from left to right. +3.The advantages of using pointers is that they are very powerful and help a person work with arrays. It is also a way to access a variable without having to call for it. +4.1 char * +4.2 invalid +4.3 invalid +4.4 10 +4.5 0 +4.6 12 +4.7 2 +4.8 char +4.9 0 +4.10 6 diff --git a/reverse.c b/reverse.c new file mode 100644 index 0000000..96c6581 --- /dev/null +++ b/reverse.c @@ -0,0 +1,36 @@ +#include +#include + +void stringReversal(char *input) +{ + char *ptrBegin = input, *ptrEnd = ptrBegin + strlen(input) - 1; + for (; ptrBegin <= ptrEnd; --ptrEnd) + { + if (*ptrEnd == '\n') + { + continue; + } + printf("%c", *ptrEnd); + + } + +} + +int main() +{ + printf("Write anything you want!\n"); + char input[1000] = {}; + int i; + fgets(input, sizeof(input), stdin); + for (i = 0; i < strlen(input); i++) + { + if (input[i] == '\n' || input[i] == '\0') + { + i--; + break; + } + } + stringReversal(input); + printf("\n"); + return 0; +} diff --git a/string.c b/string.c new file mode 100644 index 0000000..a499b1c --- /dev/null +++ b/string.c @@ -0,0 +1,52 @@ +#include +#include +void concat(char *ptr1, char *ptr2) +{ + char *ptr3 = ptr1; + while (*ptr3 != '\0') + { + ptr3++; + } + while (*ptr2 != '\0') + { + *ptr3 = *ptr2; + ptr3++; + ptr2++; + } + ptr3++; + *ptr3 = '\0'; +} +int compare(char *ptr1, char *ptr2) +{ + int flag=0; + + while(*ptr1!='\0' && *ptr2!='\0'){ + if(*ptr1 != *ptr2){ + flag=1; + break; + } + ptr1++; + ptr2++; + } + if (flag==0 && *ptr1=='\0' && *ptr2=='\0') + { + return 0; + } + else if (*ptr1 > *ptr2) + { + return 1; + } + else if (*ptr2 > *ptr1) + { + return -1; + } + +} +int main() +{ + char ptr1[12] = "Hello ", ptr2[] = "World"; + concat(ptr1, ptr2); + printf("%d\n", compare(ptr1,ptr2)); + printf("%s\n", ptr1); + return 0; +}