题目一:
请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。比如:字符串“abacacde”过滤结果为“abcde”
code(C).1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void deleter_char(const char *pInputStr, int Len, char *pOutputStr);
void main()
{
char pInputStr[100];
char pOutputStr[100];
int Len;
printf("请输入一个字符串\n");
gets(pInputStr);
printf("去重之前的字符串%s\n",pInputStr);
Len = strlen(pInputStr);
deleter_char(pInputStr,Len,pOutputStr);
printf("去重之后的字符串%s\n",pOutputStr);
}
void deleter_char(const char *pInputStr, int Len, char *pOutputStr)
{
int i,j = 1,k;
if(pInputStr == NULL || pOutputStr == NULL || Len < 0)
{
printf("输入有误!\n");
exit(0);
}
pOutputStr[0] = pInputStr[0];
for(i = 1;i < Len;i++)
{
for(k = 0;k < j;k++)
{
if(pInputStr[i] == pOutputStr[k])
break;
}
if(k == j)
{
pOutputStr[j] = pInputStr[i];
j++;
}
}
pOutputStr[j] = '\0';
}
Code(C).2
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void deleter_char(const char *pInputStr, int Len, char *pOutputStr);
void main()
{
char pInputStr[100];
char pOutputStr[100];
int Len;
printf("请输入一个字符串\n");
gets(pInputStr);
printf("去重之前的字符串%s\n",pInputStr);
Len = strlen(pInputStr);
deleter_char(pInputStr,Len,pOutputStr);
printf("去重之后的字符串%s\n",pOutputStr);
}
void deleter_char(const char *pInputStr, int Len, char *pOutputStr)
{
int Hash[128],i,j = 0; //用空间换时间
if(pInputStr == NULL || pOutputStr == NULL || Len < 0)
{
printf("输入有误!\n");
exit(0);
}
for(i = 0;i < 128;i++)
Hash[i] = 0;
for(i = 0;i < Len;i++)
Hash[pInputStr[i]]++;
for(i = 0;i < Len;i++)
{
if(Hash[pInputStr[i]] != 0)
{
pOutputStr[j++] = pInputStr[i];
Hash[pInputStr[i]] = 0;
}
}
pOutputStr[j] = '\0';
}
题目二:
编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。压缩规则:仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc". 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"
Code(C):
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void stringZip(const char *pInputStr, int Len, char *pOutputStr);
char *num2str(int count);
void reverse(char *p);
void main()
{
char pInputStr[100];
char pOutputStr[100];
int Len;
printf("请输入一个字符串\n");
gets(pInputStr);
printf("压缩之前的字符串%s\n",pInputStr);
Len = strlen(pInputStr);
stringZip(pInputStr,Len,pOutputStr);
printf("压缩之后的字符串%s\n",pOutputStr);
}
void stringZip(const char *pInputStr, int Len, char *pOutputStr)
{
int i,j = 0,count = 1,num_len,m;
char temp,*p = NULL;
if(pInputStr == NULL || Len <= 0 || pOutputStr == NULL)
{
printf("输入有误!");
exit(0);
}
temp = pInputStr[0];
for(i = 1;i < Len;i++)
{
if(pInputStr[i] == temp)
{
count++;
}
else
{
if(count > 1)
{
p = num2str(count);
num_len = strlen(p);
for(m = 0;m < num_len;m++)
{
pOutputStr[j] = p[m];
j++;
}
}
pOutputStr[j++] = temp;
temp = pInputStr[i];
count = 1;
}
}
if(count > 1)
{
p = num2str(count);
num_len = strlen(p);
for(m = 0;m < num_len;m++)
{
pOutputStr[j] = p[m];
j++;
}
}
pOutputStr[j++] = temp;
pOutputStr[j] = '\0';
}
char *num2str(int count)
{
int m = count,i = 0;
char p[20],*q = p;
while(m)
{
p[i] = m%10+'0';
m /=10;
i++;
}
p[i] = '\0';
reverse(p);
return q;
}
void reverse(char *p)
{
int s = 0,e = strlen(p)-1;
char c;
while(s<e)
{
c = p[s];
p[s] = p[e];
p[e] = c;
s++;
e--;
}
}