finding something in a string

Hi Experts,

I have a program that reads from a file and populates a struct. It contains DVD titles, directors, etc...

I am able to load the information from the text file into the struct no problems.

What i want to be able to do, is ask the user to input a string and for my function to search the title array in the DVDMovies struct and return any matches.

I would also like it to be case insensitive. That is:

If the user types in:
rush hour

i should retreive:
rush hour
rush hour 2

in all cases.

thanks in advanced,
sexy-sxAsked:
Who is Participating?
 
ankuratvbConnect With a Mentor Commented:
Use strstr() to search for a substring in a string:

Here's an exmaple:

#include <stdio.h>
#include <string.h>

int main(void)
{
   char *str1 = "Borland International", *str2 = "NaTion", *ptr;
   ptr = strstr(strlwr(str1),strlwr(str2));
  //Converted the string & sub-str to lower case to make search case insensitive
   if(ptr!=NULL)
       printf("Found");
   else
       printf("Not Found");
   return 0;
}
0
 
tzxie2000Commented:
struct DVDstru
{
  char title[256];
  //other information
};

int main()
{
  char input[256];
  struct DVDstru dvdlist;
  int dvdcount;

  //loadfile to dvdlist
  for(;;)
  {
    printf("please enter the DVD title(n to end):");
    //use scanf to read to input string
    if (strcmp(input,"n")==0) break;
    for(i=0;i<dvdcount;i++)
    {
         //if found break;
    }
    if(i<dvdcount)
       //found dvd information and output
    else
       // not found print something

  }
}
0
 
sexy-sxAuthor Commented:
is the function strlwr ansi compatible?
0
Become a Leader in Data Analytics

Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.  

 
ankuratvbCommented:
No.But you can always implement your own.
0
 
sexy-sxAuthor Commented:
i've tried this:

char* strlwr(char *_src)
{
   char *_c = _src;
   
   while ( *_src++ )
   {
      tolower(*_src);
   }

   return _c;
}

but it keeps giving me a segmentation fault
0
 
ankuratvbCommented:
char* strlwr(char *_src)
{
 char *temp;
 char *_c ;
 temp=(char*)malloc((strlen(_src)*sizeof(char))+1);
 strcpy(temp,_src);
 _c=temp;
  do
  {
     *temp=tolower(*temp);
  }while ( *temp++ );
  return _c;
}
0
 
ankuratvbCommented:
The strlwr() code you posted modifies the original string passed to it,so if it is not modifiable,i.e. stored in read only memory,the program will segfault.

For e.g.:
char *str1 = "Borland International";

The literal "Borland International" may be stored in read only memory so if you try to modfiy its content,the program will segfault.

You can use :
char str[]="Borland International";

to prevent storing it in Read only memory.
0
 
ankuratvbConnect With a Mentor Commented:
See this as well:

http://c.snippets.org/snip_lister.php?fname=strupr.c

This modifies the original as well,so you need to ensure that the string passed to it is not stored in read only memory.
0
 
sexy-sxAuthor Commented:
cool, it's all working.
thank guys
0
All Courses

From novice to tech pro — start learning today.