473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

extracting substrings based on 'fuzzy' match

This problem was raised in comp.lang.perl.misc, and the poster was
concerned, among other things, by the speed of execution.
Since C is faster than perl, I wonder how a C coder would solve it?

Given a *very* long string, A, and a shorter string, B, extract all
substrings of A that differ from B in at most N positions.

For example:
N = 2
A = 'abcdefaacdefxbcxfaaabcdefaaaacdxf'
B = 'abcdef'
the substrings are 'abcdef' at offsets 0 & 19, 'aacdef' at offset 6, and
'aacdxf' at offset 27.

Nov 21 '08 #1
2 2797

"Pilcrow" <Pi******@gmail.comwrote in message
news:9p********************************@4ax.com...
This problem was raised in comp.lang.perl.misc, and the poster was
concerned, among other things, by the speed of execution.
Since C is faster than perl, I wonder how a C coder would solve it?

Given a *very* long string, A, and a shorter string, B, extract all
substrings of A that differ from B in at most N positions.

For example:
N = 2
A = 'abcdefaacdefxbcxfaaabcdefaaaacdxf'
B = 'abcdef'
the substrings are 'abcdef' at offsets 0 & 19, 'aacdef' at offset 6, and
'aacdxf' at offset 27.
Here's how /I/ did, as an occasional C coder; this assumes substrings can
overlap:

#include <stdio.h>
#include <string.h>

int main(void) {
char *a="abcdefaacdefxbcxfaaabcdefaaaacdxf";
char *b="abcdef";
int n=2;
int blen;
int i,j,k,c;

blen=strlen(b);

k=strlen(a)-blen+1;

for (i=0; i<k; ++i) {

/* Compare substring a[i]..a[i+blen-1] with b*/
c=0;
for (j=0; j<blen; ++j)
if (a[i+j]!=b[j]){
++c;
if (c>n) break;
}
if (c<=n) {
printf("Offset: %d Substring: ",i);
for (j=0; j<blen; ++j) printf("%c",a[i+j]);
puts("");
}
}
}

--
Bartc

Nov 21 '08 #2
Pilcrow wrote:
This problem was raised in comp.lang.perl.misc, and the poster was
concerned, among other things, by the speed of execution.
Since C is faster than perl,
What a nonsensical assertion!
[...] I wonder how a C coder would solve it?

Given a *very* long string, A, and a shorter string, B, extract all
substrings of A that differ from B in at most N positions.

For example:
N = 2
A = 'abcdefaacdefxbcxfaaabcdefaaaacdxf'
B = 'abcdef'
the substrings are 'abcdef' at offsets 0 & 19, 'aacdef' at offset 6, and
'aacdxf' at offset 27.
I don't see anything C-specific about the question.
A few ideas come to mind, but they'd be better suited to
a general programming or algorithms group than to this one.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 21 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: spam | last post by:
Is there a well-known algorithm for replacing many substrings in a string? For example, I'd like to take the string "abc def ghi jkl mno pqr" and replace, say, every instance of "abc", "ghi", and...
1
by: Evaluating Fuzzy SW | last post by:
Hi All We are using soundex (and later tried Nysiis) for fuzzy name search software. But we faced a lot of problems the search accuracy was not very good also we saw a lot of misses of...
3
by: Will McGugan | last post by:
Hi, Is there a simple way of replacing a large number of substrings in a string? I was hoping that str.replace could take a dictionary and use it to replace the occurrences of the keys with the...
3
by: War Eagle | last post by:
I've been looking at .substring and .trim methods and I still have a question about extracting substrings from a textbox. Basically the textbox contains the full path to a file ... for example ...
3
by: michael.f.morrison | last post by:
Hi all, Does anyone now if there is a way to do a fuzzy string compare in c#? For example, comparing 'cat' to 'cats' would return something like a 90% match ratio. I was told that this type of ...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
24
by: BBands | last post by:
I have some CDs and have been archiving them on a PC. I wrote a Python script that spans the archive and returns a list of its contents: ...]. I wanted to add a search function to locate all the...
4
by: ashilyas | last post by:
Hi all, I am new to this forum as well as Visual Basic. I haven't been able to find a resource that tells me what functions are available in Visual Basic like you can find the listing in C++...
1
by: Jason S | last post by:
Is there a way to get the position of multiple substrings that match a regexp without using closures? match() returns the substrings themselves, not the positions, and search() seems to only return...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.