String filtering--Huawei written test

Source: Internet
Author: User

First, the title description (60 points):
Enter a string of lowercase letters (A~Z) through the keyboard. Write a string filter to filter out characters that are not the first occurrence if multiple identical characters appear in the string.
For example, the string "ABACACDE" filter result is "ABCDE".

Required implementation function: void Stringfilter (const char *PINPUTSTR, long Linputlen, Char *poutputstr);

"Input" PINPUTSTR: input string
Linputlen: Input string length
"Output" POUTPUTSTR: output string, space has been opened up, and input string length;

"Note" Only needs to complete the function algorithm, the middle does not need any IO input and output

Example
Input: "DEEFD" Output: "Def"
Input: "AFAFAFAF" Output: "AF"
Input: "PPPPPPPP" Output: "P"

The main function has been hidden, which is reserved for the user's test portal, where you can test your implementation function to invoke printf printout
Currently you can use other methods to test, as long as the final program can be correctly executed, the function implementation can be arbitrarily modified, but do not change the function prototype. Make sure that the compile run is not affected.

The first idea: think of a large array to store the input value, and then for the loop, and then find the same value, assign it 0, not the same as the last output is not 0 of the array value.

This way of thinking, is unable to meet the requirements of the topic, to implement their own functions, and do not use cin,cout,scanf,printf IO input and output functions. So this method has to be discarded.

Improved thinking: (1) pointers and arrays can be used interchangeably, pointers and array;

(2) usually with the character, the first occurrence and so on, can be set up a hash function to store the number of 26-letter occurrences;

(3) Break through the traditional IO input and output, use the method of function to realize;

The procedure is as follows:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>
void Stringfilter (const char* PINPUTSTR, Long Linputlen, char* poutputstr);


#define MAXCHAR 256
int main ()
{
Char pinputstr1[] = {"Aabcbdedxyjlz"};
Char Poutputstr1[maxchar] = {0};
Stringfilter (PINPUTSTR1, strlen (PINPUTSTR1), POUTPUTSTR1);
Puts (POUTPUTSTR1);
System ("pause");
return 0;
}

void Stringfilter (const char* PINPUTSTR, Long Linputlen, char* poutputstr)

{
BOOL Hash[26]={0}; Show 26-bit letters which have appeared
int i,j;
for (I=0,j=0;i<linputlen; i++)
{
if (hash[pinputstr[i]-' a ']==false)//This point has not yet appeared
{
hash[pinputstr[i]-' a ']=true; Mark the letter has appeared
Poutputstr[j++]=pinputstr[i]; Letters that don't appear are assigned to the output array.
}
}
poutputstr[j]= ' + ';
}

String filtering--Huawei written test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.