"String processing algorithm" to remove the specific characters of the algorithm design and C code implementation

Source: Internet
Author: User

I. Description of requirements

Enter a long string and a short string, and the program will remove the characters from the long string that appear in the short string.

For example, the long string is "1234ABCD", the short string is "3a", then the string after the program is "124BCD", and if the long string is "good Bye", the short string is "OBH", then the string after the program is "GD Ye".

Second, the algorithm design

We can tell if a character should be removed from a long string by comparing the characters in the long string to the word typeface in the short string.

That is, if the long string is "1234ABCD" and the short string is "2a", then the first character "1" in the long string is compared with "2" and "a" in the short string respectively, and the character "1" is added to the new string, then the second character in the long string "2" Compared with the "2" and "a" in the short string, the character "2" is not added to the new string, so it is executed until all the characters in the long string are more complete.

III. Special Process Considerations

In the process of writing the program, we have to consider the length and format of the input string, such as:

1. If one of the two strings entered contains Chinese characters, the program returns directly without performing the subsequent process.

2. If the length of the input short string is greater than the length of the long string, the program returns directly without performing the subsequent process.

Iv. Code of the program

Five, the procedure test

We will write the program "REMOVECHARS.C" upload to the Linux machine, and use the "Gcc-g-o removecharsremovechars.c" command to compile the program, generate "Removechars" file. The program is tested in detail below.

1. Enter the long string as "1234ABCD" and the short string "2a" when the program is running as follows:

Please input the long string:

1234abcd

Inputlongstr=1234abcd

Please input the short string:

2a

inputshortstr=2a

Remove chars of 2a from 1234ABCD, the new str IS:134BCD

2. Enter the long string as "Happy dog!" and the short string "AO" when the program is running as follows:

Please input the long string:

Happy dog!

Inputlongstr=happy dog!

Please input the short string:

Ao

Inputshortstr=ao

Remove chars of ao from Happy dog!, the new str is:hppy dg!

3. Enter the long string as "We 123", the short string is "345", the program runs as follows:

Please input the long string:

we are 123

inputlongstr= we are 123

Please input the short string:

345

inputshortstr=345

we are 123 have Chinese character, please check!

4. Enter a long string of "12345" and a short string of "234567" when the program runs as follows:

Please input the long string:

12345

inputlongstr=12345

Please input the short string:

234567

inputshortstr=234567

234567 is longer than 12345, please check!

5. Enter the long string as "ABCDSF" and the short string "af2" when the program runs as follows:

Please input the long string:

Abcdsf

Inputlongstr=abcdsf

Please input the short string:

af2

Inputshortstr=af2

Remove chars of af2 from ABCDSF, the new str Is:bcds

Vi. Expansion of demand

Based on the requirements and procedures in this article, we can consider the following extensions to the requirements:

1. If a character in a short string exists in a long string, a space placeholder is used at the corresponding position in the long string instead of being removed directly from the long string.

2. Do not limit the input string can not appear in the Chinese characters, that is, if the long string is "We 123", the short string is "I 1", then the program after processing the string is "23".

"String processing algorithm" to remove the specific characters of the algorithm design and C code implementation

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.