I. Description of requirements
Enter a string, write the program to find the longest consecutive character in the string, and determine the number of consecutive occurrences.
For example, the longest consecutive character in "123444445" is 4, and its number of consecutive occurrences is 5; the longest consecutive character in "Abcddef" is D, which is 2 consecutive occurrences, and the longest consecutive character in "AB" is a, which is a continuous occurrence of 1.
Second, the algorithm design
We can take the method of comparing each character in a string to get the longest consecutive character and the number of successive occurrences. 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 the program, we have to consider the length and format of the input string, such as:
1. If the input string contains spaces, then we only process the string before the space.
2. The input string can contain numbers, letters, and some special characters, but cannot contain Chinese characters.
Iv. Code of the program
Five, the procedure test
We will write the program "LONGESTCONTINCHARS.C" upload to the Linux machine, and use the "Gcc-g-olongestcontinchars longestcontinchars.c" command to compile the program, generate " Longestcontinchars "file. The program is tested in detail below.
1. When the input string is "Ababbba", the program runs as follows:
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", the program runs as follows:
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 runs 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", the program runs as follows:
Please input the string:
ABCdef
Inputstr=abcdef
the characters with the most consecutive occurrences are : A
the number of consecutive occurrences is : 1
Vi. Expansion of demand
Based on the requirements and procedures in this article, we can consider the following extensions to the requirements:
1. The case of the letter is not distinguished, that is, if the input string is "aaaaaa", then the program output the longest consecutive character is a or a, the number of occurrences is 6.
2. Do not limit the string can not appear in the Chinese characters, that is, if 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