Solved

finding something in a string

Posted on 2004-09-18
9
222 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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 conditional statements in the C programming language.

790 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