diff --git a/assignment7.txt b/assignment7.txt new file mode 100644 index 0000000..0c34356 --- /dev/null +++ b/assignment7.txt @@ -0,0 +1,24 @@ +Bettina Benitez + +1. ++*p reads right to left. Therefore, it increments point p. Same with *++p, the order of operation is right to left. But point p is incremented then the dereferenced. *p++ is read left to right therefore it is incremented then dereferenced. + +2. No the order of precedence is not guaranteed + +3. advantages: +- it points to a specific lovation in memory +- provides direct access to memory +- reduces storage and complexity + +4 + - 4.1: *char + - 4.2: invalid + - 4.3: invalid + - 4.4: *int pointer to the array + - 4.5: *char reference to the array + - 4.6: *int + - 4.7: reference to the pointer + - 4.8: incrementing the char argv + - 4.9: reference to main + - 4.10: part of the fgets fucntion to get the string. + + diff --git a/reverse.c b/reverse.c new file mode 100644 index 0000000..03c24f8 --- /dev/null +++ b/reverse.c @@ -0,0 +1,26 @@ +//Bettina Benitez +#include +#include + +void getReverse (char *x) { + int i; + int length = strlen(x); //gets the length of the string + char *first = x; //points to the first character of the string + char *last = x + length - 1; //points to the last character (not \0) of the string + char temp; // temp is used as a place holder when switching letters + for (i = 0; i < length/2; i++) { //length is divided by 2 because it only needs to go half way to swap + temp = *first; + *first = *last; + *last = temp; + *first ++; + *last--; + } + printf("%s", x); +} + +int main () { + printf("Enter a word: "); + char word [100]; + fgets(word, sizeof(word), stdin); + getReverse (word); +} diff --git a/strpoint.c b/strpoint.c new file mode 100644 index 0000000..32ae9fd --- /dev/null +++ b/strpoint.c @@ -0,0 +1,40 @@ +//Bettina Benitez +#include +#include + +void strCat (char *a, char *b) { + char *p = a; + while (*p != '\0') { + p++; + } + strcat(a, b); + *p--; + *p = ' '; + printf("%s", a); +} + +void strCmp(char *a, char *b) { + if (strcmp(a, b) == 0) + printf("Words are the same\n"); + else + printf("Words are the same\n"); +} + +int main () { + char aUserIn[100], bUserIn[100], strOpt[100]; + printf("Enter first word: "); + fgets(aUserIn, sizeof(aUserIn), stdin); + printf("Enter second word: "); + fgets(bUserIn, sizeof(bUserIn), stdin); + printf("Choose to concatenate or compare: "); + fgets(strOpt, sizeof(strOpt), stdin); + char con[100] = "concatenate\n"; + char com[100] = "compare\n"; + if (strcmp(strOpt, con) == 0) + strCat (aUserIn, bUserIn); + else if (strcmp(strOpt, com) == 0) + strCmp(aUserIn, bUserIn); + else + printf("Error: %s is not an option\n", strOpt); + return 0; +}