Hi Tina,

So, you want it without using STL calls.
Now, I have quite a few things to discuss :

(1) What exactly do you want your func to return ?? Please answer the example :
Let StringA = "THIS IS COOL"
And String B = "THIS IS NOT COOL"
What do you want your func to return ??

I think if you want to return "Intersection", you should return : "CHILOST" (only the common characters). But, you code (though has a small error) returns "THIS IS COOL".

(2) The second thing I wanna say is, the error in your code :

Look carefully :

Code:
char* intersection(char*a, char* b)
{
    static char temp[20];
    int i = 0;
    if ( (a==NULL) || (b==NULL))
        exit(0);
    else
    {
        while ( a != NULL)
        {
            if(strchr(b, *a))
            {
                temp[i] = *a;
                i++;
                a++; // here is the error
            }
        }
    }
    return temp;
}
You see the error ? When a character in a is not found in b, the program goes into an infinite loop !! So, the correct code will be :


Code: C++
char* intersection(char*a, char* b)
{
    static char temp[20];
    int i = 0;
    if ( (a==NULL) || (b==NULL))
        exit(0);
    else
    {
        while ( a != NULL)
        {
            if(strchr(b, *a))
            {
                temp[i] = *a;
                i++;
            }
            a++;
        }
    }
    return temp;
}

(3) The third thing, I think I have a better code .. (probably O(m+n))

Code: C++
#include <cstring>
#include <stdio.h>
#include <ctime>

char* Intersection_1(char* a, char* b)
{
    static char temp[300];
    int i = 0;
    if ( (a==NULL) || (b==NULL))
        return NULL;
    else
    {
        while ( *a != '\0')
        {
            if (strchr(b, *a))
            {
                temp[i] = *a;
                i++;
            }
            a++;
        }
    }

    return temp;
}

char* Intersection_2(char* StringA, char* StringB)
{
    static char C[300];
    char* pC = C;
    static char Table[2][200];
    int EndOfA = strchr(StringA, '\0')-StringA;
    int EndOfB = strchr(StringB, '\0')-StringB;

    for (int i=0; i < EndOfA; ++i)   (*(*Table+StringA[i]))++;
    for (int i=0; i < EndOfB; ++i)   (*(*(Table+1)+StringB[i]))++;

    for (int i=32; i < 127; ++i)
    {
        if ( (*(*Table+i)) && (*(*(Table+1)+i)) )
            *(pC++) = i;
    }
    *pC = '\0';

    return C;
}


int main()
{
    clock_t t1, t2;

    char A[200] = "THIS PROGRAM IS WRITTEN BY SASWAT PADHI. THIS IS THE FIRST TEST STRING. *&%$*%#*$%#(%^)(UFSUDEIRTGITuffifiiiugiu length = 125";
    char B[200] = "SGSYEDBSDJDISYD#**EFHHWSKDLJD_))@#**** RANDOM DATA IN THIS STRING **** YEOYRHWEJQPWEI[QIWEQJWDBKDFB,SDBLHSYWERYWOYR8423RH LENGTH = 134";
    char* C;
    float diff;

    t1 = clock();
    for (int i = 0; i < 100000; ++i)       C = Intersection_1(A,B);
    t2 = clock();
    diff = ((float)t2 - (float)t1) / 1000.0F;
    printf("\nC = \"%s\"\nAlgo 1 --> Running Time for 100000 iterations : %lf sec\n", C, diff);


    t1 = clock();
    for (int i = 0; i < 100000; ++i)       C = Intersection_2(A,B);
    t2 = clock();
    diff = ((float)t2 - (float)t1) / 1000.0F;
    printf("\n\nC = \"%s\"\nAlgo 2 --> Running Time for 100000 iterations : %lf sec\n", C, diff);

    return 0;
}

The program compares two LONG strings 1 lakh (10^5) times. The output :

Code:
C = "THIS PROGRAM IS WRITTEN BY SASWAT PADHI THIS IS THE FIRST TEST STRING **#*#
)FSDEIRTGIT  = 12"
Algo 1 (Your Algo)--> Running Time for 100000 iterations : 1.218000 sec


C = ")*12=ABDFGHIMNOPRSTWY"
Algo 2 (My Algo)--> Running Time for 100000 iterations : 0.344000 sec

Last edited by SaswatPadhi; 24Jul2009 at 05:53.. Reason: Typo