Thread: help for 'search in text'

  1. #1
    Banned
    Join Date
    May 2011
    Posts
    3

    help for 'search in text'

    I need a function that searches the text for a pattern. Report all occurrences of a string...
    i write this function but it doesn't work..
    thank you for alll

    Code:
    void Search(char fileName[],char array[])
    {
         char search[LENGTH];
         int i,m,n;
                 
         printf("please enter a string:\n");
         gets(search);
         
         printf("enter the start point and length:");
         scanf("%d%d",&m,&n);
         
         
         
          
         for(i=m;i<n;i++)
         {
           if(search[i]==array[i])
           {printf("%s", array[i]);}
           else
           {}
         }
            
         system("PAUSE");
         printf("\n\n");
         fflush(stdin);   
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Exactly what are you searching for?

    Search in a single string is dead simple... Look up strstr() ... it finds a string inside a string and returns a pointer.

  3. #3
    Banned
    Join Date
    May 2011
    Posts
    3
    actually a string i mean word like "apple"

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So ok... if someone enters "I want to eat an apple after my lunch"...

    Code:
    char entry[500];  // I want to eat an apple after my lunch
    char find[20];    // apple
    
    if (strstr(entry,find))
      printf("YA freaking hoo, it worked!");

  5. #5
    Banned
    Join Date
    May 2011
    Posts
    3
    so you say that only this code lines can be replace with mine out of order code ^^

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ruhi View Post
    so you say that only this code lines can be replace with mine out of order code ^^
    No... I'm showing you how to use strstr() a part of the standard C library... look it up!

    If all you're looking for is a conformation it's there, yes my minimal code sample will help you. If you need to do something else with it it here are other functions in the string library that will help you ... Look it up in your C language documentation.

    How you write your final code is up to you... but your search will only find a single character and thus isn't string matching.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Search text
    By Muscovy in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2009, 04:49 PM
  2. search text from file
    By bradleyd in forum C++ Programming
    Replies: 11
    Last Post: 07-07-2008, 10:54 AM
  3. search for text
    By MarlonDean in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2008, 02:02 AM
  4. Search text in file
    By MarlonDean in forum C++ Programming
    Replies: 10
    Last Post: 05-16-2008, 03:51 AM
  5. Search for color or text?
    By XunTric in forum C++ Programming
    Replies: 6
    Last Post: 11-11-2005, 06:03 PM