teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コードの改善

2020/09/02 13:43

投稿

kazuma-s
kazuma-s

スコア8222

answer CHANGED
@@ -32,15 +32,13 @@
32
32
  for (int i = 0; i < n; i++) {
33
33
  int c = tolower((unsigned char)city[i]);
34
34
  if (!isalpha(c)) continue;
35
- int count = 0;
35
+ int j = i;
36
- for (int j = 0; j < i; j++)
37
- if (c == tolower((unsigned char)city[j]))
36
+ while (--j >= 0 && c != tolower((unsigned char)city[j])) ;
38
- count++;
39
- if (count != 0) continue;
37
+ if (j >= 0) continue;
40
38
  if (string > base) *string++ = ',';
41
39
  *string++ = c;
42
40
  *string++ = ':';
43
- for (int j = i; j < n; j++)
41
+ for (j = i; j < n; j++)
44
42
  if (c == tolower((unsigned char)city[j]))
45
43
  *string++ = '*';
46
44
  }