• C

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

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.

HeavenChouAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AVaulinCommented:
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
pc012197Commented:
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
HeavenChouAuthor Commented:
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
jos010697Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HeavenChouAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.