const char *const ch
为什么要2个const?分别是什么意思?顺便求详细说一下这个指针具体是什么意思?
编辑下,完整程序如下:
#include
#include
#include
#include
int find_char(const int, const char *const, const int);
int main(int argc, char argv) {
bool translate = false, squeeze = false, delete = false;
char *chars1, *chars2;
if (++argv == '-') {
if (*++*argv == 's')
squeeze = true;
else if (**argv == 'd')
delete = true;
else {
printf("Unknown option -%s\n", *argv);
return EXIT_FAILURE;
}
++argv;
}
if (delete) {
if (argc != 3) {
printf("-d should be followed by exactly one string.\n");
return EXIT_FAILURE;
}
}
else if (squeeze) {
if (argc == 4)
translate = true;
else if (argc != 3) {
printf("-s should be followed by one string or two.\n");
return EXIT_FAILURE;
}
}
else if (argc != 3) {
printf("Without any option,"
" exactly two strings should be given as arguments.\n");
return EXIT_FAILURE;
}
else
translate = true;
chars1 = *argv;
if (translate && strlen(chars1) != strlen(chars2 = *++argv)) {
printf("The strings provided as arguments should have equal lengths.\n");
return EXIT_FAILURE;
}
int current, previous = '\0';
int nb_of_chars = strlen(chars1);
while ((current = getchar()) != EOF) {
if (delete) {
if (find_char(current, chars1, nb_of_chars) == nb_of_chars)
putchar(current);
continue;
}
int i = find_char(current, chars1, nb_of_chars);
if (translate) {
if (i != nb_of_chars)
current = chars2[i];
if (squeeze && (i = find_char(current, chars2, nb_of_chars)) != nb_of_chars)
current = chars2[i];
}
if (squeeze && i != nb_of_chars && current == previous)
continue;
putchar(current);
previous = current;
}
return EXIT_SUCCESS;
}
int find_char(int current, const char *const chars, const int nb_of_chars) {
for (int i = 0; i < nb_of_chars; ++i) {
if (current == chars[i])
return i;
}
return nb_of_chars;
}