Solved

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

Posted on 1997-06-04
5
271 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C: GetDiskFreeSpaceEx() for default drive 14 114
C language IDE – Compilers installation 14 75
convert char array to number in c 5 86
Windows Server 2012  Backup - HyperV host 18 64
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…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

830 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