• C

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
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:)
Who is Participating?
ozoConnect With a Mentor Commented:
#include <string.h>
int find(char *wholeString, char *searchString){
   return strstr(wholeString, searchString) != 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.

Hi William,

Is this a homework assignment?

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

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:)
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:)
>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;
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.
william007Author Commented:
*so we can called it like this
We can manipulate it in a while loop.
sunnycoderConnect With a Mentor Commented:
1. - right

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.
sunnycoderConnect With a Mentor Commented:
william007Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.