Hello,
I am wondering whether anybody could help me or point me in the right direction for this problem. Please find the problem and the solution i have written. I would like to know whether this can be improved in any way or any other alternative solution. I tried my level best to follow the guidelines. If i have not please accept my apologies as it is my very first post in this forum.
The problem is :
And the solution isCode:Do not make use of third party or non standard language libraries. Declare any dependencies on standard language libraries. Array Search C Write a function, arraysearch, that searches through a sorted array of C strings for a specified string, using the following function definition: int searcharray(char ** array, int arraySize, char * searchString); The function should return the 1-based index of the matching element in the array, or the value 0 if the search string cannot be found. The parameters are as follows: Parameter Description array Sorted array of C strings arraySize Number of elements in the array searchString C string being searched Assumptions/Constraints: The strings in the array are sorted in ASCII alphabetical order. The array and each of the elements of the array between 0 and arraySize-1 are valid pointers. The array may potentially contain a large number of elements, and the function should be optimized for speed. The array will not contain any duplicate elements. Examples: char * array[5] = { "abc" "efg", "ijk", "lmno", "pqrs" }; searcharray(array, 5, "abc") returns 1 searcharray(array, 5, "six") returns 0 String Compare C Write a function, strspcmp, that compares two C strings ignoring any trailing spaces, using the following function definition: int strspcmp(const char * s1, const char * s2); The function should return an integer value according to the following table: Case Return Value String s1 is less than s2 -1 String s1 is equal to s2 0 String s1 is greater than s2 1 Assumptions/Constraints: You may not use a regular expression library. Do not use string function from C standard library. Examples: mystrcmp( "Hello", "Hello" ) returns 0 mystrcmp( "Hello", "World" ) returns -1 mystrcmp( "World", "Hello" ) returns 1
Once again thank you very much for you help in advanceCode:int arraysearch(char** my_array, int array_size, char* search_string) { int start_pos = 0, end_pos = 0, curr_pos = 0, iterations = 1, prev_curr_pos = 0; end_pos = array_size; while (1) { curr_pos = (end_pos - start_pos) / 2 + start_pos; if (prev_curr_pos == curr_pos) { return 0; break; } prev_curr_pos = curr_pos; if (strspcmp(search_string,my_array[curr_pos]) == 0) { return (curr_pos + 1); break; } else if (strspcmp(search_string,my_array[curr_pos]) > 0) { start_pos = curr_pos + 1; } else { end_pos = curr_pos - 1; } } } int mystrlen(const char* s1) { int i; for (i=0;s1[i] != '\0';i++); return i; } int strspcmp(const char* s1, const char* s2) { int source_string_length = 0; int loop_counter = 0; int target_string_length = 0; if ((s1 == NULL) || (s2 == NULL)) { printf("Invalid string supplied"); return -999; } source_string_length = mystrlen(s1); target_string_length = mystrlen(s2); for(loop_counter=0;s1[loop_counter] == s2[loop_counter] && loop_counter < source_string_length;loop_counter ++); if (loop_counter == target_string_length && loop_counter == source_string_length) { return 0; } else { if (s1[loop_counter] > s2[loop_counter]) { return 1; } else { return -1; } } }
Regards
Raj



LinkBack URL
About LinkBacks



