View Code of Problem 22

#include "stdio.h"
#include "string.h"

void main()
{
	char a[1000],b[1000],c[1000];
	int i,j,k;
	char temp;
	for(;gets(a)!=NULL;)
	{
		gets(b);
		for(k=0,i=0;a[i]!='\0';i++)
		{
			for(j=0;b[j]!='\0';j++)
			{
				if(a[i]==b[j])
				{
					c[k]=a[i];
					k++;
				}
			}
		}
		c[k]='\0';
		if(c[k-1]==NULL)				//没有一样的
			continue;
		for(i=0;i<strlen(c);i++)			//排序
		{
			for(j=0;j<strlen(c)-i-1;j++)
			{
				if(c[j]>c[j+1])
				{
					temp=c[j];c[j]=c[j+1];c[j+1]=temp;
				}
			}
		}
		for(i=0;c[i]!='\0';i++)				//去重
		{
			if(c[i]==c[i+1])
			{
				for(j=i;c[j]!='\0';j++)
				{
					c[j]=c[j+1];
				}
				i--;		//继续从这个位置往后找
			}
		}
		for(i=0;c[i]!='\0';i++)				
		{
			printf("%c",c[i]);
		}
		printf("\n");
	}
}

Double click to view unformatted code.


Back to problem 22