#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

namespace X{
	void out(const char *str1, const char *str2, char *str3, int m, int n, int index)
	{
		if (!m && !n)
			std::cout<<str3<<std::endl;

		if (m) {
			str3[index] = str1[0];
			out(str1+1, str2, str3, m-1, n,index+1);
		}
		if (n) {
			str3[index] = str2[0];
			out(str1,str2+1, str3, m, n-1, index+1);
		}
	}

	bool isstringsum (const char *str1, const char *str2, const char *str3) {
		while (*str3) {
			if(*str1==*str3) ++str1;
			else if(*str2==*str3) ++str2;
			else return false;
			++str3;
		}

		if(*str1||*str2)
			return false;
		return true;
	}
}

int main()
{
	char *str1 = "ab";
	char *str2 = "c";
	
	int m = strlen(str1);
	int n = strlen(str2);

	char *strout= (char*) malloc((m+n+1)*sizeof(char));
	strout[m+n] = '\0';
	
	std::cout<<"result:"<<std::endl;
	X::out (str1, str2, strout, m,n, 0);
	
	char *str3 = "cab";
	X::isstringsum(str1,str2,str3)?std::cout<<"ok"<<std::endl:std::cout<<"error"<<std::endl;
	
	free(strout);
    return 0;
}