Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assignment7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Oscar So
1. First one means, ++(*p) adds one before implementing pointer. Second one is (*P++), but precedence of ++ is treated before P. Last one is similar to the second one, but it is treated as (*++P) where the compiler looks for associativity.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! 👍

2. I think both do not guarentee operator precedence, this is because in postfix, &&, ||, mathematical functions, comparison, the computer code reads it from left to right. However, for conditions, assignment of variables, and prefixes, the code goes from right to left.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! 💯

3. Advantages of pointers are when handling arrays, and data tables. They allow references to functions, and can return multiple variables and values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! 👍

4. I am a bit clueless on number 4 and would like some explanation. Thanks.
28 changes: 28 additions & 0 deletions reverse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<stdio.h>
#include<string.h>
void reverse(char *input){
int length, i;
char *start, *stop, temp;
length = strlen(input);
start=input;
stop=input;
for(i=0;i<length-1;i++){
stop++;
}
for(i=0;i<length/2;i++){
temp=*stop;
*stop=*start;
*start=temp;
start++;
stop--;
}
}
int main() {
char input[200];
printf("Please input string: \n");
fgets(input,sizeof(input),stdin);
strtok(input,"\n");
reverse(input);
printf("The reverse is: \n");
printf("%s \n", input);
}
24 changes: 24 additions & 0 deletions string1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<stdio.h>
#include<string.h>
char *cat (char *p1, char *p2){
int i=0, p3= strlen(p1);
while(p2[i]!='\0'){
p1[p3]=p2[i];
p3++;
i++;
}
p1[p3]='\0';
return p1;
}

int main(){
char p1[50], p2[50];
printf("Enter first string: ");
fgets(p1,sizeof(p1),stdin);
printf("Enter second string: ");
fgets(p2,sizeof(p2),stdin);
strtok(p1,"\n");
strtok(p2,"\n");
cat(p1,p2);
printf("New string: %s \n",p1);
}
37 changes: 37 additions & 0 deletions string2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<stdio.h>
#include<string.h>
int compare(char *p1, char *p2){
while (*p1==*p2){
if (*p1 == '\0' || *p2 == '\0'){
printf("Dissimilar User Input \n Error \n");
break;
}
p1++;
p2++;
}
if (*p1=='\0' && *p2 == '\0'){
return 0;
}else if (*p1=='\0'){
return 1;
}else if (*p2 == '\0'){
return -1;
}
}
int main () {
int result;
char p1 [100], p2 [100], final;
printf("Input first string: ");
fgets(p1, sizeof(p1),stdin);
printf("Input second string: ");
fgets(p2, sizeof(p2),stdin);
strtok(p2, "\n");
strtok(p1, "\n");
result = compare (p1,p2);
if (result == 0){
printf("Both strings are the same. \n");
}else if(result == -1) {
printf("Input 1 is longer than Input 2. \n");
}else if (result ==1) {
printf("Input 1 is shorter than Input 2. \n");
}
}