If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Procedure to Compare 2 Strings with the following criteria

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.

// 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);

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.

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 pseudo-code above could be improved, but that's not the point. The issue is to make sure the pseudo-code 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 pseudo-code 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 non-programmer (but logical thinker) to solve the problem. Maybe the non-programmer would write it more in English than the pseudocode I produced, but it would be similar in steps in what I wrote.

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.

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.

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.

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.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.