Enter a string of lowercase letters (~ Z. Compile a character string filter program. If multiple identical characters appear in the string, filter out non-first-time characters.
For example, the "abacacde" character string is "ABCDE ".
Required implementation function: void stringfilter (const char * pinputstr, long linputlen, char * poutputstr );
[Input] pinputstr: input string
Linputlen: length of the input string
[Output] poutputstr: Output string. The space has been opened up and the length of the input string is equal to that of the input string;
[Note] You only need to complete the function algorithm, and there is no IO input or output in the middle.
Example
Input: "deefd" output: "def"
Input: "afafaf" output: "AF"
Input: "pppppppp" output: "P"
The main function has been hidden. This is the test entry reserved for users. Here, you can test your implementation function and call printf to print the output.
Currently, you can use other methods to test the function. You only need to ensure that the final program can be correctly executed. The function implementation can be modified without changing the function prototype.
Make sure that compilation and running are not affected.
# Include <iostream> # include <cassert> using namespace STD; bool g_flag [26]; void stringfilter (const char * pinputstr, long linputlen, char * poutputstr) {assert (pinputstr! = NULL); int I = 0; If (pinputstr = NULL | linputlen <= 1) {return;} const char * P = pinputstr; while (* P! = '\ 0') {If (g_flag [(* P-'A')]) {// check whether the * P-'A' position is 0 each time, if it is already 1, it indicates that it has already appeared. If the output of else is 1, skip it, and the output is 0, set this position to P ++ ;} else {poutputstr [I ++] = * P; g_flag [* P-'a'] = 1; p ++ ;}} poutputstr [I] = '\ 0';} int main () {memset (g_flag, 0, sizeof (g_flag); char input [] = "aabcbbcde "; char * output = new char [strlen (input) + 1]; stringfilter (input, strlen (input), output); cout <output <Endl; // use cout to automatically output the string based on the default recognition format, and the Prin TF has control options, and whether the output is a string or the first address. Delete output; return 0 ;}
Assert () checks the validity of input parameters at the beginning of the function. The header file # include <assert>; uses the character array g_flag [] to mark 26 letters. If statement judgment: if the position of * P-'A' is already 1 (true), it indicates that the else output has been executed. (1 is used to skip the next step and 0 is used to view the output) and set this position to 1.