?
Solved

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

Posted on 1997-06-04
5
Medium Priority
?
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

800 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