Character count statistics
Question description
Question source:Character count statistics_Niuke Question Ba_Niuke.com (nowcoder.com)
Write a function that counts the number of different characters contained in a string. The characters are in the ASCII code range (0~127, including 0 and 127). The newline represents the terminator and is not included in the characters. Those not within the scope will not be counted. Multiple identical characters are counted only once
For example, for the string abaca, there are three different characters a, b, and c, so the output is 3.
Data range: 1 ≤ n ≤ 500
Enter description:
Enter a line of string without spaces.
Output description:
Output the number of characters in the input string ranging from (0 to 127, including 0 and 127).
Example 1
enter:
abc
Output:
3
Example 2
enter:
aaa
Output:
1
Analysis of ideas
This question is not so much about counting the number of characters as it is about counting the types of characters. It’s like I gave you an array and asked you to find out how many numbers appear once in it.
Then there is actually more than one solution:
- Create a new character array to store the characters that have appeared, and traverse the original string to find a new one and store it in an array.
- After sorting the strings, iterate through the array and add 1 to the direct counter when encountering a new one.
But obviously the second one is simpler, so we use the second idea
First create an array and collect characters. No more talk about the old routine.
int main(){
char str[501];
gets(str);
return 0;
}
Then we need to solve our sorting problem. You can write a sorting algorithm here, but here I recommend using the qsort
function directly to save trouble and worry. Let us review it first. qsort
function
void qsort (void* base, size_t num, size_t size,int (*compar)(const void*,const void*));
Return value: No return value, only the position pointed to bybase
will be sorted
Parameter:base
, pointing to the starting point of the data we want to sort
num
, the number of data we want to sort
size
, the size of the individual data we want to sort
compar
, a function pointer, points to a size comparison function, used to tellqsort
how to compare the size of the data you provide, where the parameters of this function are two< a i=2>, the return value is intconst void*
Suppose the followingcampar
statement
int compar (const void* p1, const void* p2);
When the return value< 0
: The element pointed to by p1
will be placed in front of the element pointed by p2
Return= 0
:p1
Japanesep2
Position not changed
When the return value> 0
: The element pointed to by p1
will be placed after the element pointed to by p2
In fact, this return value will affect whether your sorting result is in forward or reverse order. If it does not meet expectations, just swap the positions.
So if we want to use the qsort
function here, we need to know how many valid characters our string stores, so we write a simple loop for statistics
int count = 0;
while (str[count]) {
count++;
}
Then we need to write a function to compare sizes. Since the nature of characters is integer, we can actually directly return the difference between the two characters.
But here are a few points to note:
void*
Type pointers must be type converted before they can be used- Don’t be confused by the return value here. Convert the pointer type to
int*
, otherwise the dereference will become access to 4 bytes - The type of parameters and the type of return value must correspond, including
const
int cmp_char (const void* p1, const void* p2) {
return *(char*)p1 - *(char*)p2;
}
Then we can callqsort
to sort
qsort(str, count, 1, cmp_char);
Finally, we count the number of types of characters. We just start from the first one and compare it with the next character. If we find that they are not equal, let our counter increment.
Here are a few points to note:
- Circulation cycle
count-1
Next - The starting number of the counter should be
1
, because there must be a type of character at the beginning, and our comparison algorithm will not add the first type of character . From the perspective of the number of loops, when there is only one character, the loop is executedcount-1
times, which is 0 times, but there is actually one character
code show as below
int num = 1;
for (int i = 0; i < count - 1; i++) {
if (str[i] != str[i + 1]) {
num++;
}
}
Then finally print ournum
Code overview
#include <stdio.h>
#include <stdlib.h>
int cmp_char (const void* p1, const void* p2) {
return *(char*)p1 - *(char*)p2;
}
int main() {
char str[501];
gets(str);
int count = 0;
while (str[count]) {
count++;
}
qsort(str, count, 1, cmp_char);
int num = 1;
for (int i = 0; i < count - 1; i++) {
if (str[i] != str[i + 1]) {
num++;
}
}
printf("%d", num);
return 0;
}