-
Couldn't load subscription status.
- Fork 20
Assignment 7 #11
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 #11
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,18 @@ | ||
| Sean Kee | ||
|
|
||
| 1. For ++*p, it is read right to left. For *p++, it first adds, then does the *p. For *++p, it also reads right to left. | ||
|
|
||
| 2. Yes, left to right or right to left is guarenteed because the compiler uses a sort of "order of operations" in it's own sense. For terms that are the same in terms of precedence, the compiler reads right to left. | ||
|
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. The advantage of using pointers is that a) You can have a string with an undefined length. b) Pass variables by reference rather than value. | ||
|
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! they also allow references to functions! |
||
|
|
||
| 4.1 char a[3] = "abc"; for a string | ||
|
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, we were looking for the data type, which is |
||
| 4.2 Invalid because you are trying to declare an array with a string, then using a - instead of a = | ||
|
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 Invalid because you are trying to set a character from a string(end of string character) to an integer | ||
|
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 perfectly valid! '\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 Pointer- int *a = variable; Points to the variable 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. the answer is 10 because it's just pointing to the first element of the array! |
||
| 4.5 Invalid because arrays already pass by refernce | ||
|
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.
|
||
| 4.6 Pointer- int *p = variable; Points to the variable 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. Since a is a pointer to the first element of the array, the increment by 2 would move the pointer two spots over to 12. so 12 is the answer. |
||
| 4.7 Refernce- scanf("%d", &variable); You are giving the function the address of the variable so it can be used in a pointer. | ||
|
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.
|
||
| 4.8 *++argv; increases the value that argv is pointing to by 1 after the operation is complete. | ||
|
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.
|
||
| 4.9 Reference- fxn(&main); you are sending the address of the main variable to the function so that it can be used as a pointer. | ||
|
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! the actual answer is |
||
| 4.10 sizeof(str); used to find the size of a string in terms of bytes. | ||
|
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 is just 8 because that's the standard size! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /*Sean Kee*/ | ||
| /*Reverse inputted string using pointers*/ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| void stringReverse(char string[]) { | ||
| char swap; | ||
| char *beginptr = &string[0]; | ||
| char *endptr = &string[strlen(string) - 1]; | ||
|
|
||
| while (beginptr < endptr) { | ||
| swap = *beginptr; | ||
| *beginptr = *endptr; | ||
| *endptr = swap; | ||
| beginptr++; | ||
| endptr--; | ||
| } | ||
|
|
||
| printf("New String: %s\n", string); | ||
|
|
||
| } | ||
|
|
||
| int main() { | ||
| char input[100]; | ||
| printf("Input a string\n#: "); | ||
| fgets(input, sizeof(input), stdin); | ||
| input[strlen(input)-1] = '\0'; | ||
| stringReverse(input); | ||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /*Sean Kee*/ | ||
| /*String Functions*/ | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| void stringcat(char *str1, char *str2) { | ||
| while (*str1 != '\n') { | ||
| str1++; | ||
| } | ||
| while (*str2 != '\n') { | ||
| *str1 = *str2; | ||
| str2++; | ||
| str1++; | ||
| } | ||
| *str1 = '\0'; | ||
|
|
||
| } | ||
|
|
||
| int stringcmp(char *str1, char *str2) { | ||
| while (*str1 != '\n' || *str2 != '\n') { | ||
| if (*str1 == *str2) { | ||
| str1++; | ||
| str2++; | ||
| } | ||
| else | ||
| return 1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| int main() { | ||
| int menu = 1; | ||
| int option; | ||
| char input1[100]; | ||
| char input2[100]; | ||
| int returned; | ||
| printf("Input String 1\n#: "); | ||
| fgets(input1, sizeof(input1), stdin); | ||
| printf("Input String 2\n#: "); | ||
| fgets(input2, sizeof(input2), stdin); | ||
|
|
||
| while (menu == 1) { | ||
| printf("\nWhat would you like to do?\n1: Concatenate Strings\n2: Compare Strings\n3: Exit\n#: "); | ||
| scanf("%d", &option); | ||
| switch(option) { | ||
| case 1: | ||
| stringcat(input1, input2); | ||
| printf("\nNew String: %s\n", input1); | ||
| break; | ||
| case 2: | ||
| returned = stringcmp(&input1, &input2); | ||
| if (returned == 0) | ||
| printf("\nIdentical Strings\n"); | ||
| else | ||
| printf("\nNot Identical Strings\n"); | ||
| break; | ||
| case 3: | ||
| menu = 0; | ||
| break; | ||
| } | ||
| } | ||
| } |
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.
Perfect answer! 💯