From a99c0652cea34a2d80b9f1addbf169f8ae87d8ea Mon Sep 17 00:00:00 2001 From: Adonay Berhe Date: Wed, 2 Oct 2019 20:55:51 -0700 Subject: [PATCH] Adding solution for questions in week 2 Signed-off-by: Adonay Berhe --- exercises/week-2.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/exercises/week-2.c b/exercises/week-2.c index bc3ab03..a1e8c92 100644 --- a/exercises/week-2.c +++ b/exercises/week-2.c @@ -17,3 +17,49 @@ * * [1] https://git-scm.com/docs/git-format-patch */ + +#include +#include + +int main(void) +{ + //Question 1 + int x = 5, y = 7; + + printf("x = %d and y = %d\n", x, y); + swapFunction(&x, &y); + printf("x = %d and y = %d\n", x, y); + + //Question 2 + const char *tempStr = "hello"; + + returnStrlen(tempStr); + printf( "\nString = %s, and length = %d.\n", tempStr, returnStrlen(tempStr) ); + + //Question 3 + char crt = 'A', *chrptr = &crt; + + printf("Value = %c, Address = %d, Size = %d\n", crt, &crt, sizeof(crt)); + printf("Value = %d, Address = %d, Size = %d\n", chrptr, &chrptr, sizeof(chrptr)); + + return 0; +} + +void swapFunction(int *one, int *two) +{ + int temp = *one; + *one = *two; + *two = temp; +} + +int returnStrlen(char *string) +{ + char* indx = (char *) malloc(sizeof(string)); + indx = string; + + while(*indx != '\0') + ++indx; + + return indx-string; +} +