Algorithm design and C code implementation of "string processing algorithm" longest consecutive characters and their occurrences

Source: Internet
Author: User

I. Description of requirements

Enter a string, write the code to find the longest consecutive character in the string, and figure out the number of consecutive occurrences.

Like what. The longest consecutive character in "123444445" is 4, and its number of occurrences is 5; the longest consecutive character in "Abcddef" is D. The number of consecutive occurrences is 2. The longest consecutive character in "AB" is a, which is 1 consecutive occurrences.

Second, the algorithm design

We were able to get the longest consecutive character and the number of successive occurrences by comparing each character in the string.

The overall process of the program 1 is seen.

Figure 1 The overall process of the program

III. Special Process Considerations

In the process of typing the code, we have to consider the length and format of the input string. Such as:

1. Assuming that the input string contains spaces, we only deal with the string before the space.

2. The input string can include numbers, letters, and some special characters. However, you cannot include Chinese characters.

Iv. Code of the program

V. Procedure Test

We will write the program "LONGESTCONTINCHARS.C" uploaded to the Linux machine. and compile the program with the "Gcc-g-olongestcontinchars longestcontinchars.c" command to generate a "longestcontinchars" file.

The following is a specific test of the program.

1. When the input string is "Ababbba". Program execution conditions such as the following:

Please input the string:

Ababbba

Inputstr=ababbba

the characters with the most consecutive occurrences are : b

the number of consecutive occurrences is : 3

2. When the input string is "123321". Program execution conditions such as the following:

Please input the string:

123321

inputstr=123321

the characters with the most consecutive occurrences are : 3

the number of consecutive occurrences is : 2

3. When the input string is "[Email protected]##$%", the program execution is as follows:

Please input the string:

[Email protected]##$%

[Email protected]##$%

the characters with the most consecutive occurrences are : #

the number of consecutive occurrences is : 2

4. When the input string is "abcdef". Program execution conditions such as the following:

Please input the string:

ABCdef

Inputstr=abcdef

the characters with the most consecutive occurrences are : A

the number of consecutive occurrences is : 1


5. When the input string is "US 123". Program execution conditions such as the following:

Please input the string:

we are 123

inputstr= we are 123

we are 123 have Chinese character, please check!


Vi. Expansion of demand

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

1. Uppercase and lowercase letters are not distinguished. That is, assuming that the input string is "AAAAAA", the program outputs the longest consecutive character as a or a. The number of occurrences is 6.

2. Do not limit the occurrence of Chinese characters in a string, that is, assume that the input string is "Can窝窝头pickles 123". Then the program output the longest consecutive character is the nest, the number of occurrences is 2.

Algorithm design and C code implementation of "string processing algorithm" longest consecutive characters and their occurrences

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.