Solved

Finding standard code for string operation

Posted on 2006-10-25
11
191 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
11 Comments
 
LVL 84

Accepted Solution

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

0
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
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
Comment Utility
Hi William,

Is this a homework assignment?

sunnycoder
0
 
LVL 9

Author Comment

by:william007
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
>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
Comment Utility
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
Comment Utility
*so we can called it like this
We can manipulate it in a while loop.
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 250 total points
Comment Utility
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 250 total points
Comment Utility
0
 
LVL 9

Author Comment

by:william007
Comment Utility
Thanks=)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to get time_t value that is a week old 6 154
Grammars for C C++ and java 1 108
Problem to scan all sheets 3 82
Line meaning 9 75
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 and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

743 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

17 Experts available now in Live!

Get 1:1 Help Now