Procedure to Compare 2 Strings with the following criteria - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: Procedure to Compare 2 Strings with the following criteria

  1. #16
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    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
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  2. #17
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Procedure to Compare 2 Strings with the following criteria

    Quote Originally Posted by Paul McKenzie View Post
    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...
    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.

  3. #18
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Procedure to Compare 2 Strings with the following criteria

    For experienced programmers this is trivial. As the OP appears to be a student, we should probably remind ourselves that posting homework solutions directly isn't how this forum works.

  4. #19
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: Procedure to Compare 2 Strings with the following criteria

    Quote Originally Posted by Eri523 View Post
    ...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;
    }
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  5. #20
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: Procedure to Compare 2 Strings with the following criteria

    Quote Originally Posted by VladimirF View Post
    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.
    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.

  6. #21
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: Procedure to Compare 2 Strings with the following criteria

    Quote Originally Posted by Eri523 View Post
    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.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  7. #22
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Procedure to Compare 2 Strings with the following criteria

    Quote Originally Posted by VladimirF View Post
    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

  8. #23
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: Procedure to Compare 2 Strings with the following criteria

    Quote Originally Posted by 2kaud View Post
    ... one possible simple way of coding it using c could be...
    Wow! Was just re-reading this thread and noticed how similar our approaches are. Honestly, I didn't even see your code before I wrote mine!
    The only differences are:
    1. I didn't do a parameter validation. That's caller's responsibility
    2. I used _strdup instead of strlen/malloc/copy combo. That's what it is here for.
    3. I did consider writing 1 into the search string (just as you did), but then I realized that I don't care what the value is, as long as it's not 0 and not one of the potential search characters, so I just used pre-incremented count. Not only it saved me one line, but also a pair of brackets

    I since replace 'for' loop with 'while' (and got rid of one local loop variable), and parametrized the threshold (and got rid of the counter). Here is how it looks now, complete with the test cases (please note that similarity with your code are coincidental):
    Code:
    #include <stdio.h>
    #include <string.h>
    
    bool match(const char* s1, char* s2, unsigned int threshold)
    {
    	while(threshold && *s1)
    		if(char* found = strchr(s2, *s1++))
    			*found = threshold--;
    	return threshold == 0;
    }
    
    void test(char* s1, char* s2)
    {
    	printf("%s when compared to %s == %s\n", s1, s2, match(s1, _strdup(s2), 3) ? "true" : "false");
    }
    
    int main()
    {
    	test("1154", "1154");
    	test("1154", "1179");
    	test("1154", "2154");
    	test("1154", "2554");
    	test("1154", "2484");
    	test("1154", "2144");
    	test("1154", "4515");
    	test("1154", "1144");
    	test("1154", "1517");
    	test("1154", "4815");
    	test("1154", "1481");
    	return 0;
    }
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  9. #24
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: Procedure to Compare 2 Strings with the following criteria

    Wow! Was just re-reading this thread and noticed how similar our approaches are. Honestly, I didn't even see your code before I wrote mine!
    Great minds...

    I thought about using _strdup/StrDup but I wanted to keep my original code generic c. In your test function, isn't there a memory leak as you are not freeing the memory allocated by _strdup?

    Also I always try to have const char * parameters if possible rather than char *. I thought about writing the count value into the search string as you did but decided not to as that would limit the threshold to a max of 31 (assuming printable characters for strings s1 and s2 and not just digits).

    I've revised my code as below

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    bool no_same(const char* key, const char* value, unsigned int threshold)
    {
    	if (char *copy = _strdup(value)) {
    		for (; *key && threshold; key++)
    			if (char *ind = strchr(copy, *key))
    				*ind = 1, threshold--;
    
    		free(copy);
    	}
    	return threshold == 0;
    }
    
    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]);
    
    	for (int 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;
    }
    Last edited by 2kaud; July 13th, 2013 at 01:56 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 2 of 2 FirstFirst 12

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center