Reputation: 123
From this code I can get the matching characters in char array. But how can I get the only non-matching characters between them. I have tried using != in if condition, but it does not seem to work. Any help there? so I can get the non-matching characters from this char array.
#include<stdio.h>
#include<conio.h>
int main(){
clrscr();
char a[50];
char b[50];
printf("Enter any value here\n");
scanf("%s",&a);
printf("Enter any value here\n");
scanf("%s",&b);
for(int a1=0;a1<=5;a1++)
{
for(int b1=0;b1<=5;b1++)
{
if(a1[a] == b1[b])
{
printf("%c",a[a1]);
}
}
}
getch();
return 0;
}
Upvotes: 1
Views: 643
Reputation: 4084
There are several possible methods, depending on what you mean by finding "the non-matching characters between them." If you want to find all characters that are in either a
or b
but not in both, a simple (but not optimal) approach is:
c
of all characters that are in both of the arrays (union).a
and find all characters that are not in c
.b
and find all characters that are not in c
.For example:
Here's one way:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char **argv ) {
char a[50];
char b[50];
char unmatched[100];
int lena, lenb, i;
int numUnmatched = 0;
printf("Enter any value here\n");
scanf("%s",a);
printf("Enter any value here\n");
scanf("%s",b);
lena = strlen(a);
lenb = strlen(b);
unmatched[0] = '\0';
for(i=0; i<lena; i++) {
if ( !strchr( b, a[i] ) ) {
if ( ! strchr(unmatched, a[i]) ) {
unmatched[numUnmatched++] = a[i];
}
}
}
for(i=0; i<lenb; i++) {
if ( !strchr( a, b[i] ) ) {
if ( ! strchr(unmatched, b[i]) ) {
unmatched[numUnmatched++] = b[i];
}
}
}
for ( i=0; i < numUnmatched; i++ ) {
printf( "%c ", unmatched[i] );
}
printf( "\n" );
return EXIT_SUCCESS;
}
Upvotes: 0
Reputation: 180998
This is correct and pretty efficient, even for far longer strings. It prints the results in collation order, however, which may or may not be suitable.
#include <stdio.h>
#include <conio.h>
#include <limits.h>
int main(){
char a[50];
char b[50];
int a_chars[UCHAR_MAX + 1] = {0};
int b_chars[UCHAR_MAX + 1] = {0};
int i;
clrscr();
printf("Enter any value here\n");
scanf("%49s%*[^\n]",&a);
printf("Enter any value here\n");
scanf("%49s%*[^\n]",&b);
for (i = 0; a[i]; i++) {
a_chars[(unsigned char) a[i]] = 1;
}
for (i = 0; b[i]; i++) {
b_chars[(unsigned char) b[i]] = 1;
}
for (i = 1; i <= UCHAR_MAX; i++) {
if (a_chars[i] ^ b_chars[i]) {
putchar(i);
}
}
putchar('\n');
getch();
return 0;
}
It scans each string to record a table of which characters it contains, then scans the two tables to find the characters in one but not the other.
Also, unlike your code, it adapts to the actual length of the input strings, up to the maximum allowed (49 characters), and does not produce undefined behavior in the event that the user enters longer strings. In the event that the user provides longer strings, the excess characters are ignored.
Upvotes: 1
Reputation: 481
The reason why it does not work is because if you do add the ! in the if statement, only the non matching characters in a are printed, but not b. Just add a line: printf("%c", b1[b])
.
Upvotes: 0