原来打算用hash直接对应,但是那样会把顺寻改变。下面给出两个程序,一个用hash,一个不是:请大家分析
//输入一个字符串中没有重复的字符如“abbbc”输出abc
void Repeat_to_Signal(char *s)
{
int hash[50],index;
memset(hash,0,sizeof(hash));
for (int i=0;i<strlen(s);i++)
{
int index=s[i]-'A';
hash[index]++;
}
for (i=0;i<50;i++)
{
if (hash[i]!=0)
{
printf("%c",i+'A');
}
}
}
void Repeat_to_Singal1(char *s)
{
int temp[256],k=0,*p,j;
memset(temp,'\0',sizeof(temp));
temp[k++]=s[0];
for (int i=0;i<strlen(s);i++)
{
for (j=0,p=temp;j<256;j++,p++)
{
if (s[i]==temp[j]) //如果有重复则直接跳出来
{
break;
}
if (*p=='\0') //如果进行到末尾,还没找到相同的则说明,前面的没有这个字符。直接在尾部添加
{
temp[k++]=s[i];
break;
}
}
}
for (p=temp;*p!='\0';p++)
{
printf("%c",*p);
}
}