//题目:在字符串中找出第一个只出现一次的字符。 //如输入“abaccdeff\0”,则输出‘b‘。 #ifndef __SELECT_FIRST__ #define __SELECT_FIRST__ char Select_first(char *p,int size); char Select_first1(char *p, int size); char Select_first2(char *p, int size); #endif //头文件 函数声明 #include<stdio.h>//函数体 #include<string.h> char Select_first(char arr[],int size) { int i = 0, j = 0; while (arr[i]) { if ((arr[i] != arr[j]&&j<(size-1))||i==j) { j++; } else { j = 0; i++; } if (j == (size - 1)) return arr[i]; } return ‘\0‘; } char Select_first1(char *p, int size) { char ch = ‘\0‘; char arr[20]; int i = 1,j=1; strcpy(arr, p); while (arr[0]) { ch = arr[0]; i = 1; j = 1; while (arr[i]) { if (ch == arr[i]) { j++; } else { arr[i - j] = arr[i]; } i++; } if (j == 1) return ch; arr[i - j] = ‘\n‘; } return ‘\0‘; } char Select_first2(char p[]) { int arr[255] = { 0 }; char * ptr = NULL; ptr = &p[0]; while (*p) { arr[*p]++; p++; } while (*ptr) { if (arr[*ptr] == 1) return *ptr; ptr++; } return ‘\0‘; } #include<stdio.h> //主函数 #include"Select_first.h" int main() { char arr[] = "abbaccdeff"; char ch=Select_first2(arr,strlen(arr)); printf("%c\n",ch); }
时间: 2024-11-09 06:31:32