Algorithm design and C code implementation of "String processing algorithm" string

Source: Internet
Author: User

I. Description of requirements

Given a long string and a short string, the writer determines whether all characters in a short string are in a long string. If it is, the long string contains a short string, and conversely, does not contain.


To try to include most cases, the string can contain uppercase and lowercase letters, numbers, and various punctuation marks, and is case-sensitive.


Here are a few examples to illustrate:

1. If the long string is "ABCDE" and the short string is "ADC", then all the characters in the short string are in a long string, that is, the long string contains the short string.


2. If the long string is "ABCDE" and the short string is "ADCF", then not all characters in the short string are in a long string, that is, the long string does not contain a short string.


3. If the long string is "ABCDE" and the short string is "AAB", then all the characters in the short string are in a long string, that is, the long string contains the short string.

Second, the algorithm design

As we all know, the human body is made up of cells, and a string is made up of characters. If all the characters that make up a string are in another string, the string is contained by another string.


Therefore, we might consider finding all the characters in the two strings first, and then judging whether all the characters in the shorter string appear in a longer string. If so, then two strings are contained in the relationship, and if not, then two strings are "strangers."


The overall process of the program is shown in 1.

Figure 1 The overall process of the program

III. Special Process Considerations

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

1. If the input error causes the short string length to be greater than the long string, then the program returns directly without subsequent processing.


2. Do not allow a space in the middle of the input string, if it appears, only the contents of the front of the space as the input string.


3. The input string can contain characters such as letters (case sensitive), numbers, punctuation, and so on.


4. In order to facilitate program processing, the long string is set to a maximum of 500 bytes, and the shorter string is a maximum of 100 bytes.

Iv. Code of the program

Five, the procedure test

We will write the program "STRINGCONTAINS.C" upload to the Linux machine, and use the "Gcc-g-o stringcontainsstringcontains.c" command to compile the program, generate "Stringcontains" File. The program is tested in detail below.


1. When entering a longer string of "abcdf" and a shorter string "AF", the program runs as follows:

Please input the longer string:

Abcdf

Longerstr=abcdf

Please input the shorter string:

Af

Shorterstr=af

ABCDF contains af!

2. When you enter a longer string of "AB", and the shorter string is "ABC", the program runs as follows:

Please input the longer string:

Ab

Longerstr=ab

Please input the shorter string:

Abc

Shorterstr=abc

ABC is longer than AB, please check!

3. When entering a longer string of "awe" and a shorter string "RF", the program runs as follows:

Please input the longer string:

Awe

Longerstr=awe

Please input the shorter string:

Rf

Shorterstr=rf

AWE doesn ' t contain rf!

4. When you enter a longer string of "' 11245" and a shorter string of "45", the program runs as follows:

Please input the longer string:

' 11245

Longerstr= ' 11245

Please input the shorter string:

45

Shorterstr=45

' 11245 contains 45!

5. When you enter a longer string of "123" and a shorter string of "123 45", the program runs as follows:

Please input the longer string:

123

Longerstr=123

Please input the shorter string:

123 45

Shorterstr=123

123 contains 123!

Can be seen, for the above considerations of the special circumstances, the program will be able to make the correct treatment.

Vi. Expansion of demand

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

1. Limit the input string to contain only letters, and if other characters are included, exit without processing.


2. If all characters in a shorter string are in a longer string, but a character appears in a shorter string greater than the number of occurrences in a longer string, it is assumed that the longer string does not contain a shorter string.

Algorithm design and C code implementation of "String processing algorithm" string

Related Article

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.