-
July 11th, 2013, 04:58 AM
#14
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
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
|