Asaf Tolkovsky
2012-01-16 04:07:35 UTC
(a) write a function that gets a list of strings into a 2-dimensional array of characters so in every line you'll have 1 string. The maximal length of a string is 100 characters and it can contain the space character (spaces between the words). Every string you get from the user ends with \n.
You can assume that the user will not use more than 100 characters for every string and that all the string's characters are in the form of a-z and not A-Z.
The function has to be defined as follows: void readStrings (char strings[][COL_NUM], int num);
num is the number of lines in the matrix (the number of strings) and COL_NUM is the number of columns.
(b) Write a function that gets as input an array of strings (which is represented as a 2-dimensional array of chars like in (a)), an array of pointers and the number of strings. The function has to fill the pointers array so it points to the string in the strings list: The first place points to the first string, the second place to the second string etc. You can assume that the pointers array is in suitable size. The function has to be defined as follows: void pointStrings (char strings[][COL_SIZE], char* pointers[], int num);
(c) Write a function that gets as input an array of pointers and 2 locations in the array and switches between these 2 locations. The function has to be defined as follows: void swap (char* pointers[], int i, int j);
(d) Write a function that gets as input an array of pointers, its size, and a certain location in the array. Every pointer points to a string. The function returns the location of the pointer that points to the first string in lexicographic order from that location to the end of the array.
*Lexicography is the way a dictionary is sorted. In order to check who comes first use strcmp from string.h. This function gets as input 2 strings and returns 0 if they are identical, a negative number if the first priors the second Lexicographically and a positive number if the second priors the first Lexicographically.
The function has to be defined as follows: int find_min_string (char *pointers[], int size, int start);
size is the size of the array and start is the location from which you have to find the first string lexicographically.
(e) Write a function that gets as input an array of pointers to strings and its size and sorts it with bubble sorting. The sorting is done lexicographically. The function has to be defined as follows: void bubble_sort_strings (char* pointers[], int size);
(f) Write a program which gets as input from the user 5 strings (using the function in (a)). The program allocates an array of pointers to those strings and then prints, using the array, the strings in the order they were given by the user. Then - the program sorts the pointers according to the Lexicographic order of the strings and prints the sorted strings.
Example:
Please insert 5 strings:
this is the first string
and now the second
how about the third?
two more to go
last string...
not sorted:
this is the first string
and now the second
how about the third?
two more to go
last string...
sorted:
and now the second
how about the third?
last string...
this is the first string
two more to go...