forked from CPRF-Session2/Assignment4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment4.txt
More file actions
7 lines (7 loc) · 2.27 KB
/
Assignment4.txt
File metadata and controls
7 lines (7 loc) · 2.27 KB
1
2
3
4
5
6
7
1)In C, one cannot concatenate strings through the simple addition of string1 + string2 like in other programming languages. Instead, one can use strcat(); to concatenate strings. For example, if we had string1 with a content of 'Hello' and string2 with a content of 'World' and an initialized string3, then concatenating the two strings with string3 = strcat(string1, string 2); would cause string3 to contain the contents of 'Hello World'.
2) If a static array is not initialized at all, the default values will be indeterminate, or null. However, if the static array is at least partially initialized, then the rest of the variables would be zero-initialized.
I would declare a multidimensional array with a size of 64 by creating eight arrays with an index size of 8. I would declare this by using the code, "int mdarray[8][8];".
4a) "strcmp" takes two strings and sees if they have the same or different value. If the two strings have the same value, then 0 would be returned. If string 2 comes before string 1 in lexicographical (A.K.A - alphabetical) order, then a positive number would be returned. However, if string 1 comes before string 2 in alphabetical order, then a negative number would be returned.
4b) "fgets" is a less buggy alternative to using "scanf" to allow the user to input strings. It reads the string from the keyboard, and returns what the user-inputted along with '\n', which the programmer should substitute for '\0'.
4c) "strcat" is used to concatenate strings, as aforementioned above. The programmer must first have two strings with contents for which they would like to mash together, along with a third string to store the result in. If string1 contains the word 'Hi' and string2 contains the word 'Mom', then using the code string3 = strcat(string1, string2); would end up with a result of 'Hi Mom' being stored in string3.
4d) "strlen" is used to get the size of just what is used up in the array, versus "sizeof" which is used to get the size of the entire array, regardless whether all of it is used or not. For instance, if string1[50] was scanned to have 'I play Pokemon Go', then strlen(string1) would return the number 17 (as it does not include '\0' [the NULL character]). However, sizeof(string1) would return 50, so one should not be confused as to the function of each command.