Solved

How to find the same sub-string in two strings?

Posted on 1997-06-04
5
268 Views
Last Modified: 2010-04-15
There are two strings, how to find the same sub-string?
123ABCDEFG2233... (String A)
4567ABCDEFG1232... (String B)
we can see the answer is "ABCDEFG",
how to find it? Thanks a lot.

0
Comment
Question by:HeavenChou
5 Comments
 
LVL 4

Expert Comment

by:AVaulin
ID: 1250874
ABCDEFG isn't single answer. Substrings "1", "2", "3", "12" also can be accepted as answer. You must formulate question more clearly (for example how to find same substring which length more than <n> symbols.
0
 
LVL 3

Expert Comment

by:pc012197
ID: 1250875
We *cannot* see the answer is "ABCDEFG". "ABCDEFG" may be the longest common substring, but there are more. E. g. "23" is also a common substring. "1" is also a common substring.
So please be more specific in your question.

0
 

Author Comment

by:HeavenChou
ID: 1250876
May be I can say I want fine the longest common substring.
I want write a program to compare two file that two person keyin in chinese.
so I need a program, it's easy to find which one keyin error. just like the DOS FC.EXE do , but fc only compare the line , not every word. ex.
This is a book and that is a pen. (string a)
This are a book and I has a pen. (string b)
This <is/are> a book and <that is/I has> a pen. (The result)
I hope you can know my mean. Thanks !

0
 
LVL 4

Accepted Solution

by:
jos010697 earned 100 total points
ID: 1250877
I guess this is what you want:

char*   lcs(char* a, char* b, int* l) {

        int     i, j, k, la, lb;
        char*   p= NULL;

        la= strlen(a);
        lb= strlen(b);
        *l= 0;

        for (i= 0; i < la-*l; i++)
                for (j= 0; j < lb-*l; j++) {
                        for (k= 0; k+i < la && k+j < lb; k++)
                                if (a[i+k] != b[j+k])
                                        break;
                        if (k > *l) {
                                *l= k;
                                p= a+i;
                        }
                }

        return p;

}

This function returns a pointer to the start of the largest
common substring; The last parameter contains the
length of this substring.

kind regards,

Jos aka jos@and.nl
0
 

Author Comment

by:HeavenChou
ID: 1250878
I'm sorry that I cannot show my question clearly,
but this answey for search the longest common substring
is very good!

so I change to another question (I only 104 point now)
"Find the next common substring"
I hope your answer.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

744 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

12 Experts available now in Live!

Get 1:1 Help Now