Procedure to Compare 2 Strings with the following criteria
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: Procedure to Compare 2 Strings with the following criteria

Hybrid View

1. Junior Member
Join Date
Mar 2005
Posts
13

Procedure to Compare 2 Strings with the following criteria

Procedure to Compare 2 Strings with the following criteria

Hi everyone - I hope your day is going great.

Can someone please kindly help me with the coding of the following function -
I have absolutely no clue where to start -

Given the following sets of numbers -
1154 1179 2154 2554 2484 2144 4515 1144 1517 4815 1481

Given the Index number of 1154

I want to search the numbers for the Index number of 1154

The search will return a True if I can find 3 or 4 same digits between the Index number and the 8 numbers

The search also have the following criteria -

meaning that -

1154 when compared to 1154 == true
1154 when compared to 1179 == false
1154 when compared to 2154 == true
1154 when compared to 2554 == false
1154 when compared to 2484 == false
1154 when compared to 2144 == false
1154 when compared to 4515 == true
1154 when compared to 1144 == true
1154 when compared to 1517 == true
1154 when compared to 4815 == true
1154 when compared to 1481 == true

the index number can also be of type - 1234, 1123, 1112, 1111

Thank You

2. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

Re: Procedure to Compare 2 Strings with the following criteria

Originally Posted by VENDETTA
Procedure to Compare 2 Strings with the following criteria

Hi everyone - I hope your day is going great.

Can someone please kindly help me with the coding of the following function -
That is the problem, and that is coding.

You should be first writing down on paper how to solve the problem using discrete steps, and not necessarily in C++. Then you translate those steps to code, then you implement the code.

Just jumping in and writing a C++ function without properly writing a prototype is not the way to go about doing this.

Regards,

Paul McKenzie

3. Junior Member
Join Date
Mar 2005
Posts
13

Re: Procedure to Compare 2 Strings with the following criteria

Sorry, Here is what I have done so far ...

Please do not laugh ...

Somehow the Strncmp() Function is not working ...

Any help will be well appreciated ...

Thank You ...

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <memory.h>
#include <dos.h>
#include <string.h>

/*start bubbleSort*/
void bubbleSort(int *array, int length)
{
int i, j, temp;
int test; /*use this only if unsure whether the list is already sorted or not*/
for(i = length - 1; i > 0; i--)
{
test=0;
for(j = 0; j < i; j++)
{
if(array[j] > array[j+1]) /* compare neighboring elements */
{
temp = array[j]; /* swap array[j] and array[j+1] */
array[j] = array[j+1];
array[j+1] = temp;
test=1;
}
} /*end for j*/
if(test==0) break; /*will exit if the list is sorted!*/
} /*end for i*/
}
/*end bubbleSort*/

void main(void)
{
FILE *fileptr;

int FourDDatabaseX[120][4];
int FourDRows, FourDCols;

int char1, char2, char3, charx;

int counter1, counter2, counter3;

int s, t, u, v, w, x, y, z;
int a, b, c, d, e, f, g, h, i, j, k, l, m;

int FourDVal1 ,FourDVal2, FourDVal3, FourDVal4;
int FourDValX[4];
int FourD1[5];
int FourD2[5];
int FourD1X;
int FourD2X;

char FourD1char[8];
char FourD2char[8];
char Tempbuff[8];

FourDRows=120;
FourDCols=4;

//FourD1char = (char*)malloc(sizeof(char)*8);
//FourD2char = (char*)malloc(sizeof(char)*8);

for(x=0;x!=120;x++)
for(y=0;y!=4;y++)
FourDDatabaseX[x][y]=0;

fileptr=fopen("4D Database-X.txt", "r");

x=0;
y=0;
// copy all the moses database into the 2D array moses_db[x][y]
while(!feof(fileptr))
{
fscanf(fileptr,"%1d%1d%1d%1d ",&FourDVal1,&FourDVal2,&FourDVal3,&FourDVal4);

FourDDatabaseX[x][0]=FourDVal1;
FourDDatabaseX[x][1]=FourDVal2;
FourDDatabaseX[x][2]=FourDVal3;
FourDDatabaseX[x][3]=FourDVal4;
x++;
y++;
if(y==10)
{
fscanf(fileptr,"\n");
y=0;
}
}

fclose(fileptr);

//for(x=0;x!=120;x++) printf("[%d%d%d%d]\n",FourDDatabaseX[x][0],FourDDatabaseX[x][1],FourDDatabaseX[x][2],FourDDatabaseX[x][3]);
//exit(0);

for(s=0;s!=120;s++)
{

FourD1[0]=FourDDatabaseX[s][0];
FourD1[1]=FourDDatabaseX[s][1];
FourD1[2]=FourDDatabaseX[s][2];
FourD1[3]=FourDDatabaseX[s][3];

bubbleSort(FourD1,4);

//printf("%d%d%d%d", FourD1[0], FourD1[1], FourD1[2], FourD1[3] );

for(t=0;t!=120;t++)
{
FourD2[0]=FourDDatabaseX[t][0];
FourD2[1]=FourDDatabaseX[t][1];
FourD2[2]=FourDDatabaseX[t][2];
FourD2[3]=FourDDatabaseX[t][3];

bubbleSort(FourD2,4);

//printf("D1 %d%d%d%d ", FourD1[0], FourD1[1], FourD1[2], FourD1[3] );
//printf("D2 %d%d%d%d\n", FourD2[0], FourD2[1], FourD2[2], FourD2[3] );

FourD1X = (FourD1[0]*1000) + (FourD1[1]*100) + (FourD1[2]*10) + (FourD1[3]);
FourD2X = (FourD2[0]*1000) + (FourD2[1]*100) + (FourD2[2]*10) + (FourD2[3]);

//printf("FourD2X %d\n",FourD2X);

sprintf(FourD1char,"%d",FourD1X);
sprintf(FourD2char,"%d",FourD2X);

if(strlen(FourD2char)==3)
{
strcpy(Tempbuff,"0");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}

if(strlen(FourD2char)==2)
{
strcpy(Tempbuff,"00");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}
if(strlen(FourD2char)==1)
{
strcpy(Tempbuff,"000");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}

//printf("[%s]",FourD2char);

//if((FourD1[0]==FourD2[0])&&(FourD1[1]==FourD2[1])&&(FourD1[2]==FourD2[2])&&(FourD1[3]==FourD2[3]))
// printf("FOUND[%d%d%d%d]", FourDDatabaseX[s][0], FourDDatabaseX[s][1], FourDDatabaseX[s][2], FourDDatabaseX[s][3]);

//if(strncmp(FourD1char,FourD2char,4)==0)
printf("[strncmp %d]",strncmp(FourD1char,FourD2char,4));
{
printf("[%s][%s]\n", FourD1char, FourD2char);

}

}
printf("\n");
}

}

4. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

Re: Procedure to Compare 2 Strings with the following criteria

Originally Posted by VENDETTA
Sorry, Here is what I have done so far ...
First, please use code tags when posting code. The code you posted is unformatted and unreadable without code tags.

Second, your question is not specific to Visual C++. It is a generic C++ program, so it should have been posted in the Non-Visual C++ forum.

Third, is this supposed to be a C++ program? I ask, since it looks more like a 'C' program than a C++ program.

Fourth:
Code:
`#include <dos.h>`
dos.h? Where does this come from? What version of the Microsoft Visual C++ compiler are you using?

As to code tags, look at the difference between the code below and the one you posted in terms of formatting:
Code:
```#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <memory.h>
#include <dos.h>
#include <string.h>

/*start bubbleSort*/
void bubbleSort(int *array, int length)
{
int i, j, temp;
int test; /*use this only if unsure whether the list is already sorted or not*/
for(i = length - 1; i > 0; i--)
{
test=0;
for(j = 0; j < i; j++)
{
if(array[j] > array[j+1]) /* compare neighboring elements */
{
temp = array[j];    /* swap array[j] and array[j+1] */
array[j] = array[j+1];
array[j+1] = temp;
test=1;
}
} /*end for j*/
if(test==0) break; /*will exit if the list is sorted!*/
} /*end for i*/
}
/*end bubbleSort*/

void main(void)
{
FILE *fileptr;

int FourDDatabaseX[120][4];
int FourDRows, FourDCols;

int char1, char2, char3, charx;

int counter1, counter2, counter3;

int s, t, u, v, w, x, y, z;
int a, b, c, d, e, f, g, h, i, j, k, l, m;

int FourDVal1 ,FourDVal2, FourDVal3, FourDVal4;
int FourDValX[4];
int FourD1[5];
int FourD2[5];
int FourD1X;
int FourD2X;

char FourD1char[8];
char FourD2char[8];
char Tempbuff[8];

FourDRows=120;
FourDCols=4;

//FourD1char = (char*)malloc(sizeof(char)*8);
//FourD2char = (char*)malloc(sizeof(char)*8);

for(x=0;x!=120;x++)
for(y=0;y!=4;y++)
FourDDatabaseX[x][y]=0;

fileptr=fopen("4D Database-X.txt", "r");

x=0;
y=0;
// copy all the moses database into the 2D array moses_db[x][y]
while(!feof(fileptr))
{
fscanf(fileptr,"%1d%1d%1d%1d	",&FourDVal1,&FourDVal2,&FourDVal3,&FourDVal4);

FourDDatabaseX[x][0]=FourDVal1;
FourDDatabaseX[x][1]=FourDVal2;
FourDDatabaseX[x][2]=FourDVal3;
FourDDatabaseX[x][3]=FourDVal4;
x++;
y++;
if(y==10)
{
fscanf(fileptr,"\n");
y=0;
}
}

fclose(fileptr);

//for(x=0;x!=120;x++) printf("[%d%d%d%d]\n",FourDDatabaseX[x][0],FourDDatabaseX[x][1],FourDDatabaseX[x][2],FourDDatabaseX[x][3]);
//exit(0);

for(s=0;s!=120;s++)
{

FourD1[0]=FourDDatabaseX[s][0];
FourD1[1]=FourDDatabaseX[s][1];
FourD1[2]=FourDDatabaseX[s][2];
FourD1[3]=FourDDatabaseX[s][3];

bubbleSort(FourD1,4);

//printf("%d%d%d%d", FourD1[0], FourD1[1], FourD1[2], FourD1[3] );

for(t=0;t!=120;t++)
{
FourD2[0]=FourDDatabaseX[t][0];
FourD2[1]=FourDDatabaseX[t][1];
FourD2[2]=FourDDatabaseX[t][2];
FourD2[3]=FourDDatabaseX[t][3];

bubbleSort(FourD2,4);

//printf("D1 %d%d%d%d ", FourD1[0], FourD1[1], FourD1[2], FourD1[3] );
//printf("D2 %d%d%d%d\n", FourD2[0], FourD2[1], FourD2[2], FourD2[3] );

FourD1X = (FourD1[0]*1000) + (FourD1[1]*100) + (FourD1[2]*10) + (FourD1[3]);
FourD2X = (FourD2[0]*1000) + (FourD2[1]*100) + (FourD2[2]*10) + (FourD2[3]);

//printf("FourD2X %d\n",FourD2X);

sprintf(FourD1char,"%d",FourD1X);
sprintf(FourD2char,"%d",FourD2X);

if(strlen(FourD2char)==3)
{
strcpy(Tempbuff,"0");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}

if(strlen(FourD2char)==2)
{
strcpy(Tempbuff,"00");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}
if(strlen(FourD2char)==1)
{
strcpy(Tempbuff,"000");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}

//printf("[%s]",FourD2char);

//if((FourD1[0]==FourD2[0])&&(FourD1[1]==FourD2[1])&&(FourD1[2]==FourD2[2])&&(FourD1[3]==FourD2[3]))
//	printf("FOUND[%d%d%d%d]", FourDDatabaseX[s][0], FourDDatabaseX[s][1], FourDDatabaseX[s][2], FourDDatabaseX[s][3]);

//if(strncmp(FourD1char,FourD2char,4)==0)
printf("[strncmp %d]",strncmp(FourD1char,FourD2char,4));
{
printf("[%s][%s]\n", FourD1char, FourD2char);

}

}
printf("\n");
}
}```
Last, I can't decipher your description or your example:
The search will return a True if I can find 3 or 4 same digits between the Index number and the 8 numbers
???
1154 when compared to 1154 == true
1154 when compared to 1179 == false
1154 when compared to 2154 == true
1154 when compared to 2554 == false
1154 when compared to 2484 == false
1154 when compared to 2144 == false
1154 when compared to 4515 == true
1154 when compared to 1144 == true
1154 when compared to 1517 == true
1154 when compared to 4815 == true
1154 when compared to 1481 == true
???
How exactly are you coming up with "true" or "false" from those comparisons? I don't understand what you're trying to do.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; July 10th, 2013 at 12:35 PM.

5. Junior Member
Join Date
Mar 2005
Posts
13

Re: Procedure to Compare 2 Strings with the following criteria

To determine if 2 sets of 4 digit numbers are true or false :-

Given number 1234
Given number 1243

From the second number if I can find at least 3 digits in the first number ->>> this gives a true ... but the digits can be jumbled up into any combinations ...

consideration must be taken for repeating numbers like 1123 1112 1111

Thank You

6. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

Re: Procedure to Compare 2 Strings with the following criteria

Originally Posted by VENDETTA
To determine if 2 sets of 4 digit numbers are true or false :-

Given number 1234
Given number 1243

From the second number if I can find at least 3 digits in the first number ->>> this gives a true ... but the digits can be jumbled up into any combinations ...

consideration must be taken for repeating numbers like 1123 1112 1111

Thank You
1) So is this a C++ program or C program?

2) There is no need to use a bubble sort if you're going that route. Since a bubble sort is N^2, you don't need to even consider sorting anything. Just compare digit one with all the digits in the second number and keep a counter if a digit matches one of the ones in the second number. You don't even need strcmp() or any string functions.

That's why I stated you need to have a plan first. If you did that, you should see very easily that it does not require sorting or string functions.

Regards,

Paul McKenzie
Last edited by Paul McKenzie; July 10th, 2013 at 12:52 PM.

7. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

Re: Procedure to Compare 2 Strings with the following criteria

Dude, seriously, you can do it all in just a few lines of code. That may work, but it's so far away from what it should be, you should throw it away, think about it a bit and start again. I can't even comprehend what you're thinking there.

Also, please use code tags as what you posted is unreadable
Last edited by GCDEF; July 10th, 2013 at 06:15 PM.

8. Re: Procedure to Compare 2 Strings with the following criteria

I didn't even try to understand the OP's allegedly working cryptic (even after properly auto-formatting it) code (360+ lines) from post #11, and its output I find even more mysterious:

Code:
```[1154][1154]
[1179][1179]
[2154][2154] [2154][4815]
[2554][2554]
[2484][2484]
[2144][2144]
[4515][4515]
[1144][1144]
[1517][1517]
[4815][2154] [4815][4815]
[1481][1481]
[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]
[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]
[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]
[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]
[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]

// Shortened because otherwise the post would have been too long to be accepted by the forum

[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]
[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]
[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]
[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]
[0001154][1154] [0001179][1179] [0002154][2154] [0002554][2554] [0002484][2484] [0002144][2144] [0004515][4515] [0001144][1144] [0001517][1517] [0004815][4815] [0001481][1481]```
However, mostly out of experimental curiosity, I implemented my own original idea to solve this assignment that I had rather early when reading this thread, probably already around the advent of post #1. The approach is based on comparing digit frequency histograms and the implementation (without I/O and includes) took 17 lines of C++ (rather than C, to benefit from vector range checking).

For comparison I also implemented Pauls proposed pseudocode from post #13, more or less literally translating line by line, so a real-life implementation probably would be more compact. The resulting implementation is 23 code lines, again without I/O and includes.

The overall code line count of the test implementation including both approaches and everything else is merely 77.

The results of the two approaches differ from each other, however:

Code:
```// My approach (histogram-based):

1154 when compared to 1154 == true
1154 when compared to 1179 == false
1154 when compared to 2154 == true
1154 when compared to 2554 == false
1154 when compared to 2484 == false
1154 when compared to 2144 == false
1154 when compared to 4515 == true
1154 when compared to 1144 == true
1154 when compared to 1517 == true
1154 when compared to 4815 == true
1154 when compared to 1481 == true

// Paul's approach:

1154 when compared to 1154 == true
1154 when compared to 1179 == false
1154 when compared to 2154 == true
1154 when compared to 2554 == false
1154 when compared to 2484 == false
1154 when compared to 2144 == false
1154 when compared to 4515 == true
1154 when compared to 1144 == false
1154 when compared to 1517 == false
1154 when compared to 4815 == true
1154 when compared to 1481 == false```
Looks like Paul's code, compared to the sample results from post #1, gives a few false negatives. Though I double-checked my implementation of his proposal, of course I still may have made some mistake. I think the difference results from the fact that the original assignment demands that the sequence of the matching digits in the subject pattern is to be ignored. IMO that makes the assignment rather tricky.

The histogram approach may in fact be a bit over-abstracted, but that's what I instantly felt to be the natural way to implement it.

Well, you certainly all know why I don't post any concrete (not even pseudo) code here.

Well (again), I guess that's what happens when someone feels he has too much spare time...
Last edited by Eri523; July 10th, 2013 at 11:26 PM.

9. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

Re: Procedure to Compare 2 Strings with the following criteria

Originally Posted by Eri523
Looks like Paul's code, compared to the sample results from post #1, gives a few false negatives. Though I double-checked my implementation of his proposal, of course I still may have made some mistake. I think the difference results from the fact that the original assignment demands that the sequence of the matching digits in the subject pattern is to be ignored. IMO that makes the assignment rather tricky.
Good job.

The overall point is to have a plan, even if it has flaws at the outset. Then when the plan is implemented, the same conclusion you came to would have been reached. Then adjustments to the code, plan, or algorithm can be obtained, and if necessary, speak to the teacher (if this is a course) about what was discovered and how to address it.

I started with a simple, easy to understand, explanation in English of scanning the number for a specific digit and keeping a count. Then implemented this concept to pseudocode. This is what I would expect anyone who claims "they don't know where to start" to be doing.

Regards,

Paul McKenzie

10. Re: Procedure to Compare 2 Strings with the following criteria

Originally Posted by Paul McKenzie
Good job.
Thanks.

The overall point is to have a plan, even if it has flaws at the outset. Then when the plan is implemented, the same conclusion you came to would have been reached. Then adjustments to the code, plan, or algorithm can be obtained, and if necessary, speak to the teacher (if this is a course) about what was discovered and how to address it.
The potential danger I saw was that the OP blindly trusts the algorithmic approach you proposed, ending up with a program that only partially fulfils the requirements. However, at any rate, the OP shoulf not blindly have truster his own implementation of your proposal, That woukd have made thoroughly testing the resulting program a must, and after recognizing it does not exactly do what is expected, thorough debugging woukd have been required to find out why. Result: successful learning!

I started with a simple, easy to understand, explanation in English of scanning the number for a specific digit and keeping a count. Then implemented this concept to pseudocode. This is what I would expect anyone who claims "they don't know where to start" to be doing.
Good point. It probably wouldn't be too complicated either to modify your proposed aoproach to match the requirements, I just didn't qursue that further since my own approach worked and I founf it so cool...

11. Elite Member Power Poster
Join Date
Aug 2000
Location
New York, NY, USA
Posts
5,656

Re: Procedure to Compare 2 Strings with the following criteria

Originally Posted by Eri523
...out of experimental curiosity, I implemented my own original idea to solve this assignment that I had rather early when reading this thread, probably already around the advent of post #1. The approach is based on comparing digit frequency histograms and the implementation (without I/O and includes) took 17 lines of C++ ...
17 lines?!
I can check for the "match" in 5 lines (6 if you can't mutate s2, just add strdup):
Code:
```bool match(char* s1, char* s2)
{
int count = 0;
for(char* p = s1; *p; p++)
if(char* found = strchr(s2, *p))
*found = ++count;
return count > 2;
}```

12. Re: Procedure to Compare 2 Strings with the following criteria

Originally Posted by VladimirF
17 lines?!
I can check for the "match" in 5 lines (6 if you can't mutate s2, just add strdup):

[...]
Yo, that's pretty straightforward, really cool! As I already stated, my histogram approach may be somewhat over-abstracted. In fact what I liked most about it was its genericity. But then again, how generic does it really need to get...?
Last edited by Eri523; July 11th, 2013 at 07:08 PM.

13. Elite Member Power Poster
Join Date
Aug 2000
Location
New York, NY, USA
Posts
5,656

Re: Procedure to Compare 2 Strings with the following criteria

Originally Posted by Eri523
Yo, that's pretty straightforward, really cool!
Thank you! Sometimes straightforward is the most, well, straight way to go.
I have a feeling that there is a fancy way with XOR and bit-shifting, but am too tired at the end of the week to explore.

14. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

Re: Procedure to Compare 2 Strings with the following criteria

Originally Posted by VladimirF
17 lines?!
I can check for the "match" in 5 lines (6 if you can't mutate s2, just add strdup):
Code:
```bool match(char* s1, char* s2)
{
int count = 0;
for(char* p = s1; *p; p++)
if(char* found = strchr(s2, *p))
*found = ++count;
return count > 2;
}```
The nice thing about this is that if you compare the description I gave to the OP as to how to visualize and present the solution using a plain English and logical approach, your solution matches.

This proves that it isn't a "newbie" or C++ issue, and many, if not all the posts that state "I don't know where to start" amount to nothing more than the new programmer trying to write the solution using C++ without even first thinking about the solution beforehand in these simple terms.
1) take a digit from the first number,
Code:
`for(char* p = s1; *p; p++)`
2) scan the second number for this digit
Code:
`if(char* found = strchr(s2, *p))`
3) if you find it, add 1 to a count.
Code:
`*found = ++count;`
Now, the coding would be a little "newbie-ish" if a beginner would have implemented it, but it would have been akin to the solution above. For example, there probably wouldn't have been the call to strchr(), instead a search loop of some type.

Regards,

Paul McKenzie

15. Re: Procedure to Compare 2 Strings with the following criteria

As Eri523 stated, the assignment is rather tricky. However, assuming I've correctly understood the requirements, one possible simple way of coding it using c could be

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int no_same(const char* key, const char* value)
{
int	fnd = 0;

char	*copy,
*ind;

if (key == NULL || value == NULL) return 0;

if ((copy = (char*)malloc(strlen(value + 1))) == NULL) return 0;

strcpy(copy, value);

while (*key) {
if (ind = strchr(copy, *key)) {
*ind = 1;
fnd++;
}
key++;
}

free(copy);
return fnd;
}

int main()
{
const char *vals[] = {"1154", "1179", "2154", "2554", "2484", "2144", "4515", "1144", "1517", "4815", "1481"};

const char *key = "1154";

const int noval = sizeof(vals) / sizeof(vals[0]);

int i;

for (i = 0; i < noval; i++)
printf("%s when compared to %s == %s\n", key, vals[i], no_same(key, vals[i]) >= 3 ? "true" : "false");

return 0;
}```
This gives the required output of

1154 when compared to 1154 == true
1154 when compared to 1179 == false
1154 when compared to 2154 == true
1154 when compared to 2554 == false
1154 when compared to 2484 == false
1154 when compared to 2144 == false
1154 when compared to 4515 == true
1154 when compared to 1144 == true
1154 when compared to 1517 == true
1154 when compared to 4815 == true
1154 when compared to 1481 == true

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)