Google Answers Logo
View Question
 
Q: C language programming exercise I need answer ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: C language programming exercise I need answer
Category: Computers > Programming
Asked by: lantern81-ga
List Price: $10.00
Posted: 19 Nov 2002 21:19 PST
Expires: 19 Dec 2002 21:19 PST
Question ID: 111074
I need all the C code for the following program. Design and test a
function that searches the string specified by the first function
parameter for the first occurrence of a character specified by the
second function parameter.
Have the function return a pointer to the character if successful, and
a null if the character is not found in the string. ( This duplicates
the way that the library strchr() function works.) Test the function
in a complete program that uses a loop to provide input values for
feeding to the function.
Answer  
Subject: Re: C language programming exercise I need answer
Answered By: rbnn-ga on 19 Nov 2002 23:15 PST
Rated:4 out of 5 stars
 
Thank you for your question.

If you have any questions about the code, or you would like additional
features, please use the "Request Clarification" button in your
browser to request this _before_ rating the answer.

I have to warn you that the search() function does use some C idioms
that look a little concise and mysterious at first . But one gets used
to them.

The main thing to remember here is that the string argument is a
pointer, and incrementing the pointer in the search() function does
NOT change the value of the pointer in the calling function.

I am enclosing the code for the C function you requested below. You
can also find this function at:

http://www.rbnn.com/google/search.c 

since sometimes code excerpts do not render well when cut-and-pasted.

When writing test code, a natural question is: how do we know the test
code works? Fortunately, my original version of this program had a bug
(much to my chagrin) and the test code did indeed print out a number
of nasty error messages.

Here is a sample script:
----SAMPLE SCRIPT----
% gcc search.c
% a.exe
Testing string: one
Testing string: two
Testing string: three
Testing string: 
Testing string: x
Testing string: ZZZz
Testing string:    X
Success

-----CODE FOR search.c-----
#include <stdlib.h>

/* This function takes a string as its first argument and a char as
its second.
If the first argument is NULL then NULL is returned.
Otherwise if the second argument occurs in the first argument, a
pointer to the first occurrence is returned,
otherwise NULL is returned.

The terminating 0 is considered part of the string when doing this
comparison.

*/

char * search (char * string, char sought){

  /*Check if string is NULL and if so do not do search.*/
  if (string==NULL)return NULL;

  /*Keep incrementing the string pointer until we hit a 0 character*/
  while(*string)
    if (*string==sought)return string;
    else ++string;

  /*Now string points to the terminating NULL, and we have check if
this is the char sought*/
  if (*string==sought) return string;

  /*We didn't find the char*/
  return NULL;
}

/*Test the function search() 
This function just loops through an array of strings and each char
from 0 to 126, and compares the results against strchr() .
It prints a message if it finds a mismatch.
*/

int main(int argc, char**argv){
  char*strings[]={"one","two","three","","x","ZZZz","   X"};
  int numberstrings=7;
  int error=0;
  int i=0;
  char * string;
  char c; //the character to test against
  for (i=0;i<numberstrings;i++){
    string=strings[i];
    printf("Testing string: %s\n",string);
    for (c=0;c<127;++c)
      if (search(string,c)!=strchr(string,c)){
	error=1;
	printf("Failure at %c for string: %s\n",c,string);
      }
  }
  if (error)
    printf("Failure\n");
  else
    printf("Success\n");
}

-----SEARCH STRATEGY----
I used the site:

"Notes on C" at http://www.phys.ufl.edu/~rlliu/comp/cnotes.html to
check some C syntax.

I used search keywords "C specification stdlib" to check the semantics
of strchr .
lantern81-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy