- 论坛徽章:
- 2
|
2楼的方法是最佳的了。
- /* rptchr.c */
- #include <stdio.h>
- #include <string.h>
- int repeat_char(const char *s)
- {
- char map[256];
- unsigned char c;
- memset(map, 0, sizeof(map));
- while ((c = *s++) != '\0') {
- if (map[c]) {
- return c;
- }
- ++map[c];
- }
- return -1;
- }
- int show_first_repeated_char(const char *s)
- {
- int c;
- c = repeat_char(s);
- if (c == -1) {
- printf("string %s don't have any repeated characters\n", s);
- return -1;
- }
- printf("first repeated character of string \"%s\" is '%c'\n", s, c);
- return 0;
- }
- int main(void)
- {
- show_first_repeated_char("hello world");
- show_first_repeated_char("my name is Amen");
- return 0;
- }
复制代码 |
|