Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,039 questions

52,004 answers

573 users

How to get the common characters from two strings in C++

3 Answers

0 votes
#include <iostream>

using namespace std;

bool CharacterExist(char *s, char ch)
{
	int i = 0;

	while (s[i])
		if (s[i++] == ch) return true;

	return false;
}

void GetCommonCharacters(char *s1, char *s2, char *s3)
{
	int i = 0, s1len = 0, s2len = 0;

	while (s1[i++]) s1len++;
	i = 0;
	while (s2[i++]) s2len++;
	i = 0;

	int k = 0;
	while (s1[i] || s2[i])
	{
		int j = 0;
		if (s2len > s1len)
		{
			while (s2[j])
			{
				if (s1[i] == s2[j])
					if (!CharacterExist(s3, s1[i])) s3[k++] = s1[i];
				j++;
			}
		}
		if (s1len > s2len)
		{
			while (s1[j])
			{
				if (s2[i] == s1[j])
					if (!CharacterExist(s3, s2[i])) s3[k++] = s2[i];
				j++;
			}
		}
		i++;
	}
}

int main()
{
	char arr1[10] = "GTABE", arr2[10] = "GFTFAW", arr3[10] = "";
	GetCommonCharacters(arr1, arr2, arr3);
	cout << arr3 << endl;

	char arr4[10] = "GTABEFAZ", arr5[10] = "GFTFAW", arr6[10] = "";
	GetCommonCharacters(arr4, arr5, arr6);
	cout << arr6 << endl;

	char arr7[13] = "ABZDKKSYO", arr8[13] = "WQZGDKKYSLMN", arr9[13] = "";
	GetCommonCharacters(arr7, arr8, arr9);
	cout << arr9 << endl;

	return 0;
}


/*
run:

GTA
GFTA
ZDKSY

*/

 



answered Jan 14, 2017 by avibootz
0 votes
#include <iostream>

using namespace std;

bool CharacterExist(char *s, char ch)
{
	int i = 0;

	while (s[i])
		if (s[i++] == ch) return true;

	return false;
}

void GetCommonCharacters(char *s1, char *s2, char *s3)
{
	int i = 0;
	int ascii1[256] = { 0 }, ascii2[256] = { 0 };

	while (s1[i]) ascii1[(int)s1[i++]]++;
	i = 0;

	while (s2[i]) ascii2[(int)s2[i++]]++;
	i = 0;

	int k = 0;
	while (i < 256)
	{
		if (ascii1[i] > 0 && ascii2[i] > 0)
			if (!CharacterExist(s3, ascii1[i])) s3[k++] = (char)i;
		i++;
	}
}

int main()
{
	char arr1[10] = "GTABE", arr2[10] = "GFTFAW", arr3[10] = "";
	GetCommonCharacters(arr1, arr2, arr3);
	cout << arr3 << endl;

	char arr4[10] = "GTABEFAZ", arr5[10] = "GFTFAW", arr6[10] = "";
	GetCommonCharacters(arr4, arr5, arr6);
	cout << arr6 << endl;

	char arr7[13] = "ABZDKKSYO", arr8[13] = "WQZGDKKYSLMN", arr9[13] = "";
	GetCommonCharacters(arr7, arr8, arr9);
	cout << arr9 << endl;

	return 0;
}


/*
run:

AGT
AFGT
DKSYZ

*/

 



answered Jan 14, 2017 by avibootz
0 votes
#include <iostream>

using namespace std;

bool CharacterExist(char *s, char ch)
{
	int i = 0;

	while (s[i])
		if (s[i++] == ch) return true;

	return false;
}

void GetCommonCharacters(char *s1, char *s2, char *s3)
{
	int i = 0;
	int k = 0;

	while (s1[i] || s2[i])
	{
		int j = 0;

		while (s2[j])
		{
			if (s1[i] == s2[j])
				if (!CharacterExist(s3, s1[i])) s3[k++] = s1[i];

			j++;
		}
		i++;
	}
}

int main()
{
	char arr1[10] = "GTABE", arr2[10] = "GFTFAW", arr3[10] = "";
	GetCommonCharacters(arr1, arr2, arr3);
	cout << arr3 << endl;

	char arr4[10] = "GTABEFAZ", arr5[10] = "GFTFAW", arr6[10] = "";
	GetCommonCharacters(arr4, arr5, arr6);
	cout << arr6 << endl;

	char arr7[13] = "ABZDKKSYO", arr8[13] = "WQZGDKKYSLMN", arr9[13] = "";
	GetCommonCharacters(arr7, arr8, arr9);
	cout << arr9 << endl;

	return 0;
}


/*
run:

GTA
GTAF
ZDKSY

*/

 



answered Jan 16, 2017 by avibootz

Related questions

...