還是利用字符 ASCII 定義一個表,即
static char x[256];
memset(x, 0, sizeof (x));
這樣一遍掃描即可,在掃描的過程中,首先檢測當前字符是否出現(xiàn),如果沒出現(xiàn),將對應 x 的元素置為出現(xiàn)狀態(tài),并且將該字符加入到輸出字符串中,如果已經(jīng)出現(xiàn)過,則忽略
實現(xiàn)如下:
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, 0, sizeof (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 閱讀(546)
評論(0) 編輯 收藏 引用