
July 10th, 2013, 10:01 AM
#1
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

July 10th, 2013, 10:21 AM
#2
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

July 10th, 2013, 12:03 PM
#3
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 DatabaseX.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");
}
}

July 10th, 2013, 12:31 PM
#4
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 NonVisual 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:
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 DatabaseX.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.

July 10th, 2013, 12:41 PM
#5
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

July 10th, 2013, 12:50 PM
#6
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.

July 10th, 2013, 12:52 PM
#7
Re: Procedure to Compare 2 Strings with the following criteria
But the combination factor is driving me crazy ... there are so many possible combinations ...

July 10th, 2013, 12:59 PM
#8
Re: Procedure to Compare 2 Strings with the following criteria
Originally Posted by VENDETTA
But the combination factor is driving me crazy ... there are so many possible combinations ...
You still didn't answer the question of whether it is a C or C++ program.
Also, why do you need to know the combinations? Look at the problem again, and forget about programming for a moment. Pretend you don't know one line of C++ (or C).
You have a 4 digit number and another 4 digit number. Now given that, how would you determine if 3 or more digits from the first 4 digit number are in the second 4 digit number?
Regards,
Paul McKenzie

July 10th, 2013, 01:01 PM
#9
Re: Procedure to Compare 2 Strings with the following criteria
sorry ... this should be a c program ... i keep on getting it mixed up with c++ ...
I am a newbie's newbie ...

July 10th, 2013, 02:32 PM
#10
Re: Procedure to Compare 2 Strings with the following criteria
Originally Posted by VENDETTA
But the combination factor is driving me crazy ... there are so many possible combinations ...
The number of combinations is irrelevant. As mentioned, you need to have a plan before you start coding. You have way, way, way more code than you need. The whole thing could be done in a few lines.
All you need to do is look at each character in the first string and see if it's somewhere in the second and increment a counter if it is. No sorting necessary.

July 10th, 2013, 04:54 PM
#11
Re: Procedure to Compare 2 Strings with the following criteria
I finally got it working now
Thank You very much for your time and help !!!
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <memory.h>
#include <dos.h>
#include <string.h>
int count_chars(const char* string, char ch)
{
int count = 0;
int i;
// We are computing the length once at this point
// because it is a relatively lengthy operation,
// and we don't want to have to compute it anew
// every time the i < length condition is checked.
int length = strlen(string);
for (i = 0; i < length; i++)
{
if (string[i] == ch)
{
count++;
}
}
return count;
}
void main(void)
{
FILE *fileptr;
int FourDDatabaseX[120][4];
int FourDRows, FourDCols;
int char1, char2, char3, charx;
int counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8;
int counterX[8],counterY[8];
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 DatabaseX.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];
//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];
//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(FourD1char)==3)
{
strcpy(Tempbuff,"0");
strcat(Tempbuff,FourD1char);
strcpy(FourD1char,Tempbuff);
}
if(strlen(FourD1char)==2)
{
strcpy(Tempbuff,"00");
strcat(Tempbuff,FourD1char);
strcpy(FourD1char,Tempbuff);
}
if(strlen(FourD1char)==1)
{
strcpy(Tempbuff,"000");
strcat(Tempbuff,FourD2char);
strcpy(FourD1char,Tempbuff);
}
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);
}
//int count_chars(const char* string, char ch)
//printf("FourD1X %d\n",FourD1X);
//printf("FourD2X %d\n",FourD2X);
counter1=count_chars(FourD1char,FourD1char[0]);
//printf("Counter1 %d ",counter1);
counter2=count_chars(FourD1char,FourD1char[1]);
//printf("Counter2 %d ",counter2);
counter3=count_chars(FourD1char,FourD1char[2]);
//printf("Counter3 %d ",counter3);
counter4=count_chars(FourD1char,FourD1char[3]);
//printf("Counter4 %d\n",counter4);
counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;
counter6=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==1)
counter6++;
}
if(counter6==4)
{
counter1=count_chars(FourD2char,FourD2char[0]);
//printf("Counter1 %d ",counter1);
counter2=count_chars(FourD2char,FourD2char[1]);
//printf("Counter2 %d ",counter2);
counter3=count_chars(FourD2char,FourD2char[2]);
//printf("Counter3 %d ",counter3);
counter4=count_chars(FourD2char,FourD2char[3]);
//printf("Counter4 %d\n",counter4);
counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;
counter6=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==1)
counter6++;
}
if(counter6==4)
{
counter1=count_chars(FourD2char,FourD1char[0]);
//printf("Counter1 %d",counter1);
counter2=count_chars(FourD2char,FourD1char[1]);
//printf("Counter2 %d",counter1);
counter3=count_chars(FourD2char,FourD1char[2]);
//printf("Counter3 %d",counter1);
counter4=count_chars(FourD2char,FourD1char[3]);
//printf("Counter4 %d\n",counter1);
counter5=counter1+counter2+counter3+counter4;
if(counter5 >= 3)
{
//printf("c %d ",counter5);
printf("[%s][%s] ",FourD1char,FourD2char);
}
}
}
else if((counter1==3)(counter2==3)(counter3==3)(counter4==3))
{
if((counter1+counter2+counter3+counter4) >= 7)
printf("[%s][%s] ",FourD1char,FourD2char);
}
else
{
//printf("[%s] ",FourD2char);
/*
counter1=count_chars(FourD2char,FourD2char[0]);
//printf("Counter1 %d ",counter1);
counter2=count_chars(FourD2char,FourD2char[1]);
//printf("Counter2 %d ",counter2);
counter3=count_chars(FourD2char,FourD2char[2]);
//printf("Counter3 %d ",counter3);
counter4=count_chars(FourD2char,FourD2char[3]);
//printf("Counter4 %d\n",counter4);
*/
counter1=count_chars(FourD1char,FourD1char[0]);
//printf("Counter1 %d ",counter1);
counter2=count_chars(FourD1char,FourD1char[1]);
//printf("Counter2 %d ",counter2);
counter3=count_chars(FourD1char,FourD1char[2]);
//printf("Counter3 %d ",counter3);
counter4=count_chars(FourD1char,FourD1char[3]);
//printf("Counter4 %d\n",counter4);
counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;
counter6=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==2)
counter6++;
}
if(counter6==2)
{
counter1=count_chars(FourD2char,FourD1char[0]);
//printf("Counter1 %d",counter1);
counter2=count_chars(FourD2char,FourD1char[1]);
//printf("Counter2 %d",counter1);
counter3=count_chars(FourD2char,FourD1char[2]);
//printf("Counter3 %d",counter1);
counter4=count_chars(FourD2char,FourD1char[3]);
//printf("Counter4 %d\n",counter1);
counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;
counter6=0;
counter7=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==2)
{
counter6++;
}
else if(counterX[counter5]==1)
{
counter7++;
}
}
if(counter6 >=2)
{
if(counter7 >=2)
{
printf("[%s][%s] ",FourD1char,FourD2char);
//printf("Counter %d ",counter6);
}
}
}
else if(counter6==4)
{
counter1=count_chars(FourD2char,FourD1char[0]);
//printf("Counter1 %d",counter1);
counter2=count_chars(FourD2char,FourD1char[1]);
//printf("Counter2 %d",counter1);
counter3=count_chars(FourD2char,FourD1char[2]);
//printf("Counter3 %d",counter1);
counter4=count_chars(FourD2char,FourD1char[3]);
//printf("Counter4 %d\n",counter1);
counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;
counter6=0;
counter7=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==2)
{
counter6++;
}
/*
else if(counterX[counter5]==1)
{
counter7++;
}*/
}
if(counter6 >=4)
{
//if(counter7 >=2)
{
printf("[%s][%s] ",FourD1char,FourD2char);
//printf("Counter %d ",counter6);
}
}
}
}
}
printf("\n");
}
}

July 10th, 2013, 06:11 PM
#12
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.

July 10th, 2013, 07:40 PM
#13
Re: Procedure to Compare 2 Strings with the following criteria
Originally Posted by VENDETTA
I finally got it working now
Thank You very much for your time and help !!!
What if both the numbers are 10 digits long and you want to see if 6 of those digits exist in the number? Or if the numbers are only 3 digits and you want to see if 2 digits match? Do you see where your "solution" is seriously flawed?
What GCDEF and myself are trying to get you to do is think about this in a general sense. The general solution is much simpler and I bet more understandable to a beginner than what you posted.
You simply:
1) take a digit from the first number,
2) scan the second number for this digit, and
3) if you find it, add 1 to a count.
You keep doing these step, making sure you don't "redo" a digit from the first number that was already found. That's it.
Nothing in what I just described has anything to do with C++, but with just visualizing the problem space and applying a series of discrete, logical steps to solve the problem. So being a "newbie" has really nothing to do with it in the long run. What would be "newbie" is writing a loop in C++, translating a digit from a character to a number, declaring variables, etc.
Assuming that the numbers are in string form (not straight integers, but strings representing integers), the solution is way simpler than what you posted.
1) Assume both numbers are n digits in length.
2) Assume you want to find if m digits in the first number appear in the second number, where m <= n.
Code:
int found[10] = 0 // assume this is an array of found items, and all elements are set to 0
set j = 0
set count = 0
while j < n begin
set d1 = first_number[ j ] // this is the jth digit in the first number
if ( found[d1] = 0 ) then // only do digits that have not been seen
set k = 0
while k < n begin
if ( d1 = second_number2[ k ] ) then // test against the kth digit of the second number
add 1 to count;
set found[ d1 ] = 1; // make sure we don't do this digit again
break out of while loop;
endif
add 1 to k
end while
endif
add 1 to j
end while
// output the information
if count >= m
output good
else
output bad
That is basically the entire pseudocode to the viable solution. I'll even admit that the pseudocode above could be improved, but that's not the point. The issue is to make sure the pseudocode as written will do the job, if not make adjustments and do a walkthrough by hand.
Once that makes sense, then and only then do you think about translating those steps into C++ code. This is where you need to figure out how to translate a digit to a number and back again, so that the pseudocode becomes viable, how to write a loop that goes from 0 to n, etc. This is where the "newbie" stuff comes into play.
Compare the pseudocode above to what you wrote, and compare the pseudocode above to the question I asked earlier in the post. What if the number of digits of both numbers is 10? What if the number of digits is 30? Or even 3 digits? Your "solution" will not work, while the pseudocode above works regardless of the length of the number, or the number of digits you want to match.
To be honest with you, the pseudocode is how I would expect a nonprogrammer (but logical thinker) to solve the problem. Maybe the nonprogrammer would write it more in English than the pseudocode I produced, but it would be similar in steps in what I wrote.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 10th, 2013 at 08:10 PM.

July 10th, 2013, 11:24 PM
#14
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 autoformatting 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 reallife 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 (histogrambased):
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 doublechecked 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 overabstracted, 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.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

July 11th, 2013, 04:39 AM
#15
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 doublechecked 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
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
