Find String length and Arraysearch

This is a discussion on Find String length and Arraysearch within the C Programming forums, part of the General Programming Boards category; Hello, I am wondering whether anybody could help me or point me in the right direction for this problem. Please ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Find String length and Arraysearch

    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 :

    Code:
    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
    And the solution is

    Code:
    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;
    		}
    	}
    }
    Once again thank you very much for you help in advance

    Regards

    Raj

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    1
    looks like a school assignment, if it is you gotta think about the solution your self and help can be give for your ideas or maybe if you are stuck with a problem in anyway...

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Take a few minutes to read http://cboard.cprogramming.com/annou...t.php?f=4&a=39 & http://cboard.cprogramming.com/annou...t.php?f=4&a=51

    You also haven't asked anything, you've just posted your homework.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Thanks for your encouraging replies.
    Code:
    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 don't know why every single forum (for that matter i have done in that past) always tries to criticize the problem. I know how it feels now. If you could read the above paragraph, you will understand what i have requested. Also i would like to clarify this is not a school homework/assignment. Thanks very much for your time and patience.

    Zacs7,
    I have gone through the link which you have mentioned. Thanks for that. Can i ask you, did you spend time reading the post ? Still you feel i have not followed the guidelines. Then all i can say is "English is a funny language".

    Regards

    Raj
    Last edited by s.rajaram; 10-03-2007 at 12:57 AM. Reason: Comments Added

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Are you a teacher then?
    Sorry if I was rude

    Maybe, Design a problem where you allow the student to use anything from the standard C library, I don't see why that's a bad thing - the tools exist, and they're in the real world so why not? Btw, perhaps considering bumping up the warning level on your compiler for the solution, legally all control paths must return a value (see 'arraysearch').

    Code:
    return (curr_pos + 1);
    break;
    If you return, that break will never execute, it's redundant.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Infact you are partially correct. I was a tutor back in (1996 - 1999) during my university.

    Thanks for your feedback.

    To be honest i have been given this problem to solve as a part of my pre interview question, i myself will think why do you want to create your own version of function libraries, when you have thousands of such functions which are already available and ofcourse they more optimised and efficient. However, this is what they have asked me to write. But in the end, i have been told they are not happy with it. So as a part of learning curve i want to know where i went wrong and I strongly believe in Extreme programming. Most of the time two person thoughts are the not the same. Hence I posted in this forum expecting some answers, after seeing the couple of replies, I seriously thought why did I do that ? It has completely put me off using these forums where people post/comment without even thinking about it.

    Anyways, yes your 100&#37; correct, "break" is redudant in this code. Thanks for pointing that out. Apart from that if you can come up with something else which I could have done better it will be much appreciated.

    Once again thank you very much for your time and patience.

    Enough bugging.

    Regards

    Raj
    Last edited by s.rajaram; 10-03-2007 at 02:30 AM. Reason: spelling mistakes

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21