I. Question: Find the "first" and "only once" characters in a string.
If abaccdeff is input, B is output.
Ii. Analysis of ideas:
Letter A: appears twice
Letter B: compliant with requirements
Letter C: appears twice
Letter D: appears once, but not the first
Letter e: appears once, but not the first
Letter f: appears twice
Therefore, the letter B meets the requirements.
3. How can we find qualified letters?
First, scan the character array to record the number of occurrences of each character.
Scan the character array again and find the first character with a count of 1.
Because the character (char) is a data type with a length of 8, there are a total of 256 possibilities. Therefore, we create an array with a length of 256. Each letter uses its ASCII code value as the index corresponding to the array, and the array stores the number of times each character corresponds.
4. Source Code:
# Include <iostream> <br/> # include <string> <br/> using namespace STD; <br/> void findchar (string & S) <br/>{</P> <p> int STR [256]; <br/> for (INT I = 0; I <256; I ++) // initialize the hash table <br/>{< br/> STR [I] = 0; <br/>}< br/> for (INT I = 0; I <S. size (); I ++) // use the character's ASCII value as the array subscript <br/>{< br/> STR [s [I] ++; <br/>}< br/> for (INT I = 0; I <256; I ++) // The second access character array, the first value is 1 <br/>{< br/> If (STR [I] = 1) <br/>{< br/> printf ("% C", I); <br/> return; <br/>}< br/> int main () <br/>{< br/> string S; <br/> cout <"enter a string" <Endl; <br/> CIN> S; <br/> findchar (s ); <br/> cout <Endl; <br/> return 0; <br/>}