Solved

finding something in a string

Posted on 2004-09-18
9
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

728 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