Solved

finding something in a string

Posted on 2004-09-18
9
221 Views
Last Modified: 2010-04-15
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,
0
Comment
Question by:sexy-sx
  • 5
  • 3
9 Comments
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12094159
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
 
LVL 9

Accepted Solution

by:
ankuratvb earned 125 total points
ID: 12094388
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
 

Author Comment

by:sexy-sx
ID: 12094414
is the function strlwr ansi compatible?
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 9

Expert Comment

by:ankuratvb
ID: 12094420
No.But you can always implement your own.
0
 

Author Comment

by:sexy-sx
ID: 12094423
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12094466
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12094479
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
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 125 total points
ID: 12094483
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
 

Author Comment

by:sexy-sx
ID: 12094485
cool, it's all working.
thank guys
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question