1 Replies - 868 Views - Last Post: 24 November 2009 - 02:43 PM Rate Topic: -----

#1 student1dreamincode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-June 09

need help with searching function for a string (a word in a string)

Posted 24 November 2009 - 02:04 PM

Hi, I am trying to search for a word I have entered into a list. I could list all entries so far. But I am having a hard time figuring out a function that can search for a particular word on the list. Below are the codes. I commented out different functions I have tried. Thank you!
boolean searchWord (VOCAB V[])
{
   int i;
   char target[LENGTH];

   printf("\nEnter the Word you want to search: ");
   fflush (stdin);
   gets (target);

   for ( i=0; i<SIZE; i++)
  {
	  // if ( target == V[i].Word )
	 //if ( strcmp( target, V[i].Word ) == 0);
   
	 if (strncmp (V[i].Word,"target") == 0)
	  {
	  return 0;
	   }
	 return 1;
   }
} 



Is This A Good Question/Topic? 0
  • +

#3 brds   User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: need help with searching function for a string (a word in a string)

Posted 24 November 2009 - 02:43 PM

strstr(str1, str2) returns a pointer to the location of the match in str1, NULL otherwise.

char a[] = "Its a small world";
char b[] = "small";
char *c = strstr(a, b );

printf("%s\n", c);



Output: small world.

@Edit: *#%*&^%^ Smiles!!!

This post has been edited by brds: 24 November 2009 - 02:46 PM

Was This Post Helpful? 1

Page 1 of 1