-
Couldn't load subscription status.
- Fork 20
Assignment 7 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Assignment 7 #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Yael Kelmer. | ||
|
|
||
| 1. ++*p increments the value of the datatype located at pointer p and then points at that location. *p++ points at the location in memory of p and then increments the value there. | ||
|
|
||
| 2. The left to right order is guaranteed for operator precedence. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The simple answer to this question is neither. C doesn’t always evaluate left-to-right or right-to-left. Generally, function calls are evaluated first, followed by complex expressions and then simple expressions. |
||
|
|
||
|
|
||
| 3. One advantage of pointers is that you can move along a string and point to different parts of it. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -Pointers are more efficient in handling arrays and data tables |
||
|
|
||
| 4.1 datatype: char array | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right! so |
||
| 4.2 this is invalid | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually valid! "xyz" is just an array of characters, so the "xyz"[1] is just accessing "y". Then you just subtract 'y' from it to get 0. This is because, if you can recall, a char is just a value mapped to a character! |
||
| 4.3 datatype: int. value: 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. '\0' is just a NULL terminator and by definition, NULL is equal to 0. So this would evaluate as true, which is 1 in C. |
||
| 4.4 datatype: int. value: 10 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right! |
||
| 4.5 datatype: int. value: 10 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Close! |
||
| 4.6 datatype: int. value: 12 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right! |
||
| 4.7 datatype: address. a[0] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right! which means it's just of type |
||
| 4.8 datatype: character | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Close, but not quite! It's actually |
||
| 4.9 datatype: address. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right! So the way to write this is |
||
| 4.10 datatype: int. value: 5 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The standard size is 8 - even though the array is a smaller size, 8 bytes will be allocated regardless. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* Yael Kelmer. | ||
| This code takes a user inputted string and uses a function that I created in order to print the string backwards. */ | ||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| void reversal (char userString[]) { | ||
| char *userStringStart = userString; | ||
| char *userStringEnd = userStringStart + strlen (userString) - 1; | ||
| int i; | ||
| for (i = 0; *userStringStart != *userStringEnd; i++) { | ||
| printf ("%c", *userStringEnd); | ||
| userStringEnd--; | ||
| } | ||
| printf ("%c\n", *userStringEnd); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| int main () | ||
| { | ||
| printf ("Please type a string of letters\n"); | ||
| char userString [100]; | ||
| fgets (userString, sizeof(userString), stdin); | ||
| userString [strlen (userString) - 1] = '\0'; | ||
| reversal(userString); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* Yael Kelmer. | ||
| write a program that implements two functions: strcat() and strcmp(). use pointers. write the main function to execute both of these functions on 2 strings. */ | ||
|
|
||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
|
|
||
| void stringCat (char string1[], char string2[]) { | ||
| int lengthString3 = strlen (string1) + strlen (string2); | ||
| char string3 [lengthString3]; | ||
| char *string3Ptr = string3; | ||
| char *string1Ptr = string1; | ||
| char *string2Ptr = string2; | ||
| int i; | ||
| for (i = 0; i < strlen (string1); i++) { | ||
| *string3Ptr = *string1Ptr; | ||
| string3Ptr++; | ||
| string1Ptr++; | ||
| } | ||
| for (i = 0; i < strlen (string2); i++) { | ||
| *string3Ptr = *string2Ptr; | ||
| string3Ptr++; | ||
| string2Ptr++; | ||
| } | ||
| *string3Ptr = '\0'; | ||
| printf("%s\n", string3); | ||
| } | ||
|
|
||
| int stringCmp (char string1[], char string2[]) { | ||
| int shorterLength; | ||
| if (strlen (string1) < strlen (string2)) { | ||
| shorterLength = strlen (string1); | ||
| } | ||
| else { | ||
| shorterLength = strlen (string2); | ||
| } | ||
| char *string1Ptr = string1; | ||
| char *string2Ptr = string2; | ||
| int i; | ||
| for (i = 0; i < shorterLength; i++) { | ||
| if (*string1Ptr == *string2Ptr) { | ||
| string1Ptr++; | ||
| string2Ptr++; | ||
| } | ||
| else if (*string1Ptr < *string2Ptr) { | ||
| return 1; | ||
| } | ||
| else if (*string1Ptr > *string2Ptr) { | ||
| return -1; | ||
| } | ||
| } | ||
| if (strlen (string1) == strlen (string2)) { | ||
| return 0; | ||
| } | ||
| else if (strlen (string1) < strlen (string2)) { | ||
| return 1; | ||
| } | ||
| else if (strlen (string1) > strlen (string2)) { | ||
| return -1; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| int main() { | ||
|
|
||
| char string1 [] = "hello"; | ||
| char string2 [] = "world"; | ||
| printf ("This is the concatenation of string1 and string2: "); | ||
| stringCat(string1, string2); | ||
| int comparison = stringCmp(string1, string2); | ||
| printf ("This is the comparison of string1 and string2: %d\n", comparison); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The expression ++*p has two operators of same precedence, so compiler looks for associativity. Associativity of operators is right to left. Therefore the expression is treated as ++(*p). The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. The expression *++p has two operators of same precedence, so compiler looks for associativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p).