#include "stdafx.h"
#include <stdio.h>
#include "iostream"
char s[100];
char s1[100];
int i = 0;
int i1 = 0;
int i2 = 0;
void populate();
void squeeze();
int main()
{
printf("Write something, hit enter and than write something again. Hit
enter to see result\n");
populate();
squeeze();
system("pause");
return 0;
}
void populate() // insert chars in two arrays
{
int c;
while ((c = getchar()) != '\n')
{
s[i] = c;
i++;
}
while ((c = getchar()) != '\n')
{
s1[i1] = c;
i1++;
}
}
void squeeze() // iterate the arrays to find if there are same chars
{
char s2[1000];
i = 0;
i1 = 0;
for (int r = 0; r <= s[i - 1]; r++)
{
for (int j = 0; j <= s1[i1 - 1]; j++)
{
if (s[r] == s1[j])
{
s2[i2] = s[r];
i2++;
}
}
}
for (int m = 0; m <= s2[i2 - 1]; m++) //print every element in s2[];
{
printf("%c", s2[m]);
}
}
I want to iterate through the second array s1
for every element in the first array s
and put the duplicates in the third array s2
.
For all I found on the net by far is how to iterate through two or more arrays in the same position arr1[i] = arr2[i]
, arr1[i1] = arr2[i1]
and so on. This is my code, but from unknown reasons for me it does not work.