Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1997-06-04
5
Medium Priority
?
279 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

618 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