• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Finding standard code for string operation

Hi, I wish to find the code online for
1) int find(wholeString, searchString)
find will be finding partString in the searchString, if match it will return true
let say searchString=abc
wholeString=abcde
it will return true
(Exclude the use of regular expression, only use of the standard string expression)

2)int findtextinfile(file, searchString)
findfile will find the searchString in the file, if match, it will return true,
but note that the it should not load the entire file in the memory, it should match the string with parts of the file in the memory at anytime
(Exclude the use of regular expression, only use of the standard string expression)

Note that above function signature just for illustration purpose, they are optional to look like this. Since there are fairly standard function, I think there should be many samples online, hence I do not need to reinvent the wheel. The only thing is, I don't know where to find it. Thanks for any suggestion:)
0
william007
Asked:
william007
3 Solutions
 
ozoCommented:
#include <string.h>
int find(char *wholeString, char *searchString){
   return strstr(wholeString, searchString) != 0;
}

0
 
Naveen KumarProduction Manager / Application Support ManagerCommented:
string.h can do the job for you.

strstr(..) will return pointer to the located string if found or otherwise null pointer. so you can if and else or as given by ozo to make the function return 1 or 0 OR TRUE or FALSE etc values as you wish.

Thanks
0
 
sunnycoderCommented:
Hi William,

Is this a homework assignment?

sunnycoder
0
Industry Leaders: 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!

 
william007Author Commented:
There are just the function that I needed for doing some search function. If there are assignment, there are just the assignment that wrote by me:)
For the find function, we can easily find a counterpart in other language.
For the findinfile function, I have done it once in other language...but found out that it needs lots of testing to got it right, especially the tricky part which is considering the joining part of two different buffer. Some parser in java has done it perfectly, like SAX XML parser.
I am not asking anyone to write it from scratch...just that if there are codes that available online, most likely is being tested,. We can use it directly, without wasting time to  rewrite everything, and to test everything. But since I am new in C, I don't really know where to locate such "open source" resource.
Else, I can write it by myself also:)
0
 
william007Author Commented:
Also, there are chances that C has provide such a function like ozo mentioned. Only a only newbie like me will write such an assignment:)
0
 
sunnycoderCommented:
>especially the tricky part which is considering the joining part of two different buffer.
Would the "searchString" span multiple lines? i.e. would there be a \n as part of search string? If no, then this simple function would do

int findtextinfile(fileptr, searchString)
{
      char buffer[BUF_LEN] = { 0 };

     while ( fgets(buffer, BUF_LEN, fileptr))
     {
           if ( strstr(buffer, searchString))
                return 1;
     }
     return 0;
}
0
 
william007Author Commented:
1. What is {0}? is it an expression to initialize all the char array value to be null?

2. I agree this is a solution for simple environment, but what I need is more than that:
In some circumstances, some ppl may send in xml file at  70MB - 700MB sizes without space in between; This will not be practical in handling in line since it will greatly slow down the process by dumping all the buffer into the memory. So the algorithm is something like this:
a)Get the characters in a fixed size of buffer;
b)compare that buffer with the string;
c)If not found, using the last of (searchstringlength - 1) chars as an residual for the first buffer, concat with the second buffer, and compare against the search string;
d)If found, return 1(simple cases).
To be more advance,
findtextinfile can be called multiple time, each time it will return a file pointer that matched, else return -1 if EOF met, so we can called it like this.
0
 
william007Author Commented:
*so we can called it like this
We can manipulate it in a while loop.
0
 
sunnycoderCommented:
1. - right

2.
Should not be too difficult ... Take a buffer of length n ... Divide it into two parts of length n/2 each ...
Write increment and decrement routines for accessing the buffer ... These routines would make access to buffer circular ... e.g. increment routine would be something like

char* incr ( char * current, char * buffer)
{
      if  ( current == (buffer + sizeof(buffer) -1))
             return buffer;
      return (current+1);
}

Instead of using strstr, write an implementation of your own that uses this custom incr operation.

The advantage of using two n/2 buffers instead of a single n buffer in this episode is that you can simply load half buffer at a time and using custom inc continue comparing without having to rewind to "(searchstringlength - 1)"

Be careful to check if you need to fill in a half buffer for after each call to incr.
0
 
sunnycoderCommented:
0
 
william007Author Commented:
Thanks=)
0

Featured Post

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now