Solved

finding something in a string

Posted on 2004-09-18
9
219 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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12094420
No.But you can always implement your own.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays 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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now