?
Solved

Finding standard code for string operation

Posted on 2006-10-25
11
Medium Priority
?
232 Views
Last Modified: 2010-04-15
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
Comment
Question by:william007
[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
11 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 1000 total points
ID: 17801851
#include <string.h>
int find(char *wholeString, char *searchString){
   return strstr(wholeString, searchString) != 0;
}

0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 17802246
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17803368
Hi William,

Is this a homework assignment?

sunnycoder
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Author Comment

by:william007
ID: 17803577
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
 
LVL 9

Author Comment

by:william007
ID: 17803609
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17803717
>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
 
LVL 9

Author Comment

by:william007
ID: 17804228
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
 
LVL 9

Author Comment

by:william007
ID: 17804234
*so we can called it like this
We can manipulate it in a while loop.
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 1000 total points
ID: 17804700
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
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 1000 total points
ID: 17809341
0
 
LVL 9

Author Comment

by:william007
ID: 17828660
Thanks=)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.
Suggested Courses

800 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