1. ## PLEASE!!! help intersections problems

i have to write a program that takes two inputs as arrays and then outputs the intersection.

here is what i have come up with so far.

Code:
```void main()
{
char a[7];
char b[7];
char out[7];

fgets( a, 7, stdin );

printf( "Enter your last name: " );
fgets( b, 7, stdin );

printf( "\nString 'a' %s \n", a);
printf( "\nString 'b' %s \n", lastname);

if (a[0] == b [0]) {
out[0] = a[0];
}
else if (a[1] == b [1]) {
out[1] = a[1];
}
else if (a[2] == b[2]) {
out[2] = a[2];
}
else if (a[3] == b [3]) {
out[3] = a[3];
}
else if (a[4] == b[4]) {
out[4] = a[4];
}
else if (a[5] == b[5]) {
out[5] = a[5];
}
else if (a[6] == b[6]) {
out[6] = a[6];
}

printf( "\nThe intersection is  %s \n", out);

getchar();
}```
when i run this program, if any of the if statments are met it just seems to put the whole lot in to the output (fullname)

any help welcome

2. I'm not completely sure what you are trying to achieve here. If you want 'out' to display all characters that are the same and match array positions then you will want to run a seperate 'if' check for each character to test for a match otherwise you will only be able to get the first match. You would also need to initialize the out character array as well.

Alternatively if you only want to print the first character that matches both in value and array position then this code should do what you want:
Code:
```#include <stdio.h>
int main()
{
char a[7];
char b[7];
char out='\0';

fgets( a, 7, stdin );

printf( "Enter your last name: " );
fgets( b, 7, stdin );

printf( "\nString 'a' %s \n", a);
printf( "\nString 'b' %s \n", b);

if (a[0] == b [0])
out = a[0];
else if (a[1] == b [1])
out = a[1];
else if (a[2] == b[2])
out = a[2];
else if (a[3] == b [3])
out = a[3];
else if (a[4] == b[4])
out = a[4];
else if (a[5] == b[5])
out = a[5];
else if (a[6] == b[6])
out = a[6];

printf( "\nThe intersection is  %c \n", out);

getchar();
}```
A couple of other things worth noting is that fgets() places the '\n' character in the buffer when you hit return, and also its best to use 'int main()'.

3. Code:
```#include <stdio.h>

int main()
{
char a[10];
char b[10];
char out[20];

fgets( a, sizeof a, stdin );

printf( "Enter your last name: " );
fgets( b, sizeof b, stdin );

printf( "\nString 'a' &#37;s \n", a);

/* You havn't declared lastname have you
printf( "\nString 'b' %s \n", lastname); */
printf( "\nString 'b' %s \n", b);

if (a[0] == b [0])
out[0] = a[0];

if (a[1] == b [1])
out[1] = a[1];

if (a[2] == b[2])
out[2] = a[2];

if (a[3] == b [3])
out[3] = a[3];

if (a[4] == b[4])
out[4] = a[4];

if (a[5] == b[5])
out[5] = a[5];

if (a[6] == b[6])
out[6] = a[6];

out[7] ='\0';

printf( "\nThe intersection is  %s \n", out);
getchar();
return 0;
}

/* my outout

String 'a' test

String 'b' test

The intersection is  test
*/```
Have a look at the code, give more attention on the changes done on the code. Few thing which need to be noted

1. main should return value
2. lastname wasn't declared
3. compare would have been very easy if it was implemented using loops.
4. Need more error checking

Hope that will get you start.

ssharish2005

4. i have made a small mistake on the code i put up, it should not say enter first name and last name, i want it to just work with intergers. sorry about that. il have a go now, thanks very much

5. Hope that the following code will give you an idea of how to do things..
You want to create a string with the first and last name of a person.
Thats how you may do it mate:
Code:
```#include <stdio.h>
#include <stdlib.h>

char *Interesection(char *string1, char* string2)
{
if(string1 && string2)
{
int i;
int j = 0;
//For the given exercise we apply a space between the two strings so + 1 for the space, + 1 for the '\0' = +2
char *interS = (char *)calloc(strlen(string1) + strlen(string2) + 2, sizeof(char));
if(interS)
{
for(i = 0; i < strlen(string1); interS[j++] = string1[i], i++);
interS[j++] = ' ';
for(i = 0; i < strlen(string2); interS[j++] = string2[i], i++);

//Remember that the interesection of two stings will have max the sum of the strlen characters + 1 for space
//and string[j] will show the '\0' meaning we have strlen(string1) + strlen(string2) + 2 characters.
if(j == strlen(string1) + strlen(string2)+1)
{
interS[j] = '\0';
return interS;
}
else
{
free(interS);
return NULL;
}
}
else
{
printf("Memory Error.\n");
return NULL;
}
}
else
{
printf("NULL strings.\n");
return NULL;
}
}

int main(int argc, char *argv[])
{
char string1[80] = "Cristiano";
char *string2 = "Ronaldo";
char *r = NULL;

r = Interesection(string1, string2);

printf("%s\n", r);

//Strcat does the trick but it gives no space between the strings, and need the destination first string to be large
//enough to hold the result.
strcat(string1, string2);

printf("%s\n", string1);
system("PAUSE");
return 0;
}```
Printed Results..

Cristiano Ronaldo
CristianoRonaldo
Press any key to continue . . .

Hope i helped..

6. ok, so now with some excellent help from you guys ive got this.
Code:
```
#include <stdio.h>

int main()
{
char a[10];
char b[10];
char out[20];

printf( "Please enter a Maximum of 7 whole numbers: " );
fgets( a, sizeof a, stdin );

printf( "Enter your last name: " );
fgets( b, sizeof b, stdin );

printf( "\nString 'a' &#37;s \n", a);

printf( "\nString 'b' %s \n", b);

if (a[0] == b [0])
out[0] = a[0];
else {
out[0] = 45;
}
if (a[1] == b [1])
out[1] = a[1];
else {
out[1] = 45;
}
if (a[2] == b[2])
out[2] = a[2];
else {
out[2] = 45;
}
if (a[3] == b [3])
out[3] = a[3];
else {
out[3] = 45;
}
if (a[4] == b[4])
out[4] = a[4];
else {
out[4] = 45;
}
if (a[5] == b[5])
out[5] = a[5];
else {
out[5] = 45;
}
if (a[6] == b[6])
out[6] = a[6];
else {
out[6] = 45;
}
if (a[7] == b[7])
out[7] = a[7];
else {
out[7] = 45;
}
//out[8] ='\0';

printf( "\nThe intersection is  %s \n", out);
getchar();
return 0;
}```
not a big thing, but im playing with trying to get the program to ignore eccess inputs for "a" and "b", but not with much luck.
any suggestions

7. by the way, thanks again, great help