posts - 183,  comments - 10,  trackbacks - 0

还是利用字符 ASCII 定义一个表,即

static char x[256];
memset(x, 0, sizeof (x));

这样一遍扫描即可,在扫描的过程中,首先检测当前字符是否出现,如果没出现,将对应 x 的元素置为出现状态,并且将该字符加入到输出字符串中,如果已经出现过,则忽略

实现如下:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 void changestr(const char* pIn, char* pOut)
 6 {
 7     static char x[256];
 8     int i = 0, j = 0;
 9     memset(x, 0sizeof (x));
10     for (i = 0, j = 0; i < strlen(pIn); ++i)
11     {
12         if (x[pIn[i]] == 0)
13         {
14             x[pIn[i]] = 1;
15             pOut[j] = pIn[i];
16             ++j;
17         }
18     }
19     pOut[j] = '\0';
20 }
21 
22 int main()
23 {
24     char pIn[100], pOut[100];
25     while (scanf("%s", pIn) != EOF)
26     {
27         changestr(pIn, pOut);
28         printf("%s\n", pOut);
29     }
30     return 0;
31 }

 


posted on 2011-06-27 22:45 unixfy 阅读(535) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理