diff --git a/assignment7.txt b/assignment7.txt new file mode 100644 index 0000000..95d2e3b --- /dev/null +++ b/assignment7.txt @@ -0,0 +1,14 @@ +Lloyd Page +1. ++*p dereferences the value that p points to and increments it. *p++ increments the pointer, and then dereferences it. *++p increments the pointer and then dereferences it. +2. No, the operands on the same level as prefix ++ and = read right to left,all other operands read left to right. +3. Dynamic starting size arrays, pass by reference for values, Dynamic memory allocation, provide a way to modify fuction calling arguments, more efficient use of memory and time in regards to arrays, multiple return functions, return arrays +4.1 char *, its a string, which is a char[] with a '\0', and all arrays are pointers of their respective data type +4.2 Invalid, xyz is not a declared array +4.3 1, ASCII value of '\0' is 0, 0==0 returns 1 +4.4 int, dereference of the pointer a +4.5 int *, creates a pointer assigned to a[0] +4.6 int, dereference of the pointer p +4.7 int **, creates a pointer for pointer p +4.8 char*, derefernce of the double pointer argv +4.9 Invalid, can't create a pointer to a function +4.10 5 or logic error, if called within the main function, and string.h is included, it will return 5, otherwise it will give you an error due to lack of string.h's inclusion, or logic error if called outside main. diff --git a/reverse.c b/reverse.c new file mode 100644 index 0000000..de888e1 --- /dev/null +++ b/reverse.c @@ -0,0 +1,36 @@ +/*Lloyd Page*/ +/*String reversal function with 2 pointers, user input*/ +#include +void reverse(char*,char*); +int main() +{ + char y; + do + { + char handler[100]; + printf("Enter a string\n"); + fgets(handler,sizeof(handler),stdin); + char*p=handler; + char*x=handler; + for(int i=0;handler[i]!='\n';i++) + { + x++; + } + *x='\0'; + reverse(x,p); + printf("Run Again?(y/n)"); + y=getchar(); + fgets(handler,sizeof(handler),stdin); + }while(y=='y'||y=='Y'); + return 0; +} +void reverse(char*x,char*p) +{ + while(x!=(p-1)) + { + printf("%c",*(x-1)); + x--; + } + printf("\n"); +} + diff --git a/stringf.c b/stringf.c new file mode 100644 index 0000000..6b9e62e --- /dev/null +++ b/stringf.c @@ -0,0 +1,56 @@ +/*Lloyd Page*/ +/*Implement strcat, strcmp, use pointers to execute functions on 2 strings*/ +#include +int strcmp(char*,char*); +char* strcat(char*,char*); +int main() +{ + char s1[12]="Hello"; + char s2[]="Hell"; + int x=strcmp(s1,s2); + printf("%d\n",x); + strcat(s1,s2); + printf("%s\n",s1); + return 0; +} +int strcmp(char*a,char*b) +{ + if(*a>*b) + { + return 1; + } + else + { + if(*b>*a) + { + return -1; + } + else + { + if(*a=='\0'&&*b=='\0') + { + return 0; + } + else + { + a++; + b++; + return strcmp(a,b); + } + } + } +} +char* strcat(char*a,char*b) +{ + int i=0,j=0,l=0; + while(*(a+i)!='\0') i++; + while(*(b+j)!='\0') j++; + int k=i; + while(l