Answer the question
In order to leave comments, you need to log in
How to get once those characters that are simultaneously in all three sequences?
My program accepts three sequences, but it cannot output the common characters once.
Do you need to remake the passForCommonSymbols function or suggest an algorithm for how to get those symbols that are included in all three sequences at the same time?
Task:
#include<stdio.h>
#include<locale.h>
template < typename T1, typename T2>
void passForCommonSymbols(T1* Array1, T1* Array2, T1* Array3, T2 size1, T2 size2, T2 size3)
{
char Array[100] = {0};
int p = 0;
char simbols;
for (int i = 1; i < size1; i++)
{
for (int j = 1; j < size2; j++)
{
if (Array1[i] == Array2[j])
{
for (int y = 1; y < size3; y++)
{
if (Array2[i] == Array3[y])
{
int r = 0;
while (r < p)
{
if (Array3[y] != Array[r])
{
r++;
}
if (r == p)
{
Array[p] = Array1[i];
}
}
p++;
}
}
}
}
}
printf("Общие символы: %s\n", Array);
}
int main()
{
setlocale(LC_ALL, "ru");
int k, m, n;
printf("Введите размер первой последовательности:\n");
scanf_s("%i", &k);
printf("Введите размер второй последовательности:\n");
scanf_s("%i", &m);
printf("Введите размер третьей последовательности:\n");
scanf_s("%i", &n);
getchar();
char* s = new char[k];
char* t = new char[m];
char* u = new char[n];
printf("Введите первую последовательность:\n");
fgets(s, k, stdin);
printf("Введите вторую последовательность:\n");
fgets(t, m, stdin);
printf("Введите третью последовательность:\n");
fgets(u, n, stdin);
printf("\n");
printf("Введенные последовательности:\n\n");
printf("Первая последовательность: %s\n", s);
printf("Первая последовательность: %s\n", t);
printf("Первая последовательность: %s\n", u);
passForCommonSymbols(s, t, u, k, m, n);
delete[] s;
delete[] t;
delete[] u;
}
Answer the question
In order to leave comments, you need to log in
For example:
1. Build a table of all characters unsigned char sym[256];
, fill with zeros
2. Walk through the first array: for each character encountered c
, set in the table sym[c] = 1;
3. Walk through the second array: for each character encountered c
, set in the table sym[c] |= 2;
(i.e. set the second bit to 1)
4. Walk through the third array: for each character encountered c
, set in the table sym[c] |= 4;
(i.e. set the third bit to 1)
5. Output characters with value 7 (i.e. 111 2 ) from the table:
for (size_t c = 0; c < 256; c++)
if (sym[c] == 7)
printf("%c", (unsigned char) c);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question