[String processing algorithm] design of the algorithm for maximum consecutive characters and the number of occurrences and Implementation of C code

Source: Internet
Author: User

[String processing algorithm] design of the algorithm for maximum consecutive characters and the number of occurrences and Implementation of C code

I. Requirement Description

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

For example, the maximum continuous character in "123444445" is 4, and the number of consecutive occurrences is 5; the longest continuous character in "abcddef" is d, and the number of consecutive occurrences is 2; the maximum consecutive character in "AB" is a, and the number of consecutive occurrences is 1.

Ii. Algorithm Design

We can compare the characters in a string one by one to obtain the maximum number of consecutive characters and the number of consecutive occurrences. The general process 1 of the program is shown in.

Figure 1 General Procedure

 

Iii. Special process considerations

During programming, we need to consider the length and format of the input string, for example:

1. If the input string contains spaces, we only process the string before spaces.

2. The input string can contain numbers, letters, and some special characters, but cannot contain Chinese characters.

4. program code

V. program testing

We will upload the compiled program "LongestContinChars. c" to the Linux machine, and use the "gcc-g-oLongestContinChars LongestContinChars. c" command to compile the program and generate the "LongestContinChars" file. And then perform detailed tests on the program.

1. When the input string is "ababbba", the program runs as follows:

Please input the string:

Ababbba

InputStr = ababbba

The maximum number of consecutive occurrences is B.

The number of consecutive occurrences is: 3

2. When the input string is "123321", the program runs as follows:

Please input the string:

123321

Inputstr= 123321

The maximum number of consecutive occurrences is 3.

The number of consecutive occurrences is: 2

3. The input string is "! ###$ %, The program runs as follows:

Please input the string:

! ### $ %

InputStr =! ### $ %

The most frequently-occurring characters are :#

The number of consecutive occurrences is: 2

4. When the input string is "abcdef", the program runs as follows:

Please input the string:

Abcdef

InputStr = abcdef

The maximum number of consecutive occurrences is.

The number of consecutive occurrences is: 1

5. When the input string is "We 123", the program runs as follows:

Please input the string:

We 123

InputStr = We 123

We have 123 has Chinese character, please check!

Vi. Demand Expansion

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

1. It is case-insensitive. If the input string is "AAAaaa", the maximum number of consecutive characters output by the program is A or a, and the number of occurrences is 6.

2. Chinese characters are not allowed in the string. If the input string is "123", the maximum number of consecutive characters output by the program is 2.

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.