回答編集履歴
1
ソース追記
test
CHANGED
@@ -3,3 +3,163 @@
|
|
3
3
|
また、scanf("%s",str2);は、入力文字数が30を超えると(たぶん)暴走します。fgets(str,sizeof str,stdin);を使いましょうd^^
|
4
4
|
|
5
5
|
蛇足:先の質問[if 文の周りでのエラーが解決できません。](https://teratail.com/questions/149848)でも言いましたが、小文字/数字の判定がおかしいです。
|
6
|
+
|
7
|
+
//
|
8
|
+
|
9
|
+
usr~/test/c % ./a.out
|
10
|
+
|
11
|
+
英小文字および数字を使った文字列を入力してください:1234567
|
12
|
+
|
13
|
+
入力は適切です。
|
14
|
+
|
15
|
+
再度同じ文字列を入力してください:1234567
|
16
|
+
|
17
|
+
文字列が一致しました。
|
18
|
+
|
19
|
+
usr~/test/c % ./a.out
|
20
|
+
|
21
|
+
英小文字および数字を使った文字列を入力してください:abc123
|
22
|
+
|
23
|
+
入力は適切です。
|
24
|
+
|
25
|
+
再度同じ文字列を入力してください:Abc123
|
26
|
+
|
27
|
+
文字列が不一致です。
|
28
|
+
|
29
|
+
usr~/test/c % ./a.out
|
30
|
+
|
31
|
+
英小文字および数字を使った文字列を入力してください:abc123
|
32
|
+
|
33
|
+
入力は適切です。
|
34
|
+
|
35
|
+
再度同じ文字列を入力してください:abc12
|
36
|
+
|
37
|
+
文字列が不一致です。
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
参考までに・・・
|
42
|
+
|
43
|
+
```c
|
44
|
+
|
45
|
+
usr~/test/c % cat t.c
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
#include <stdio.h>
|
50
|
+
|
51
|
+
#include <ctype.h>
|
52
|
+
|
53
|
+
#include <string.h>
|
54
|
+
|
55
|
+
//
|
56
|
+
|
57
|
+
int check(char *str);
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
int main(void)
|
62
|
+
|
63
|
+
{
|
64
|
+
|
65
|
+
char str1[30];
|
66
|
+
|
67
|
+
char str2[30];
|
68
|
+
|
69
|
+
//
|
70
|
+
|
71
|
+
fputs("英小文字および数字を使った文字列を入力してください:", stdout);
|
72
|
+
|
73
|
+
fgets(str1, sizeof str1, stdin);
|
74
|
+
|
75
|
+
int checker = check(str1);
|
76
|
+
|
77
|
+
if (checker == 0) {
|
78
|
+
|
79
|
+
puts("入力は適切です。");
|
80
|
+
|
81
|
+
} else{
|
82
|
+
|
83
|
+
if (checker == 1) {
|
84
|
+
|
85
|
+
puts("入力が不適切です。大文字は使用できません。");
|
86
|
+
|
87
|
+
} else if (checker == 2) {
|
88
|
+
|
89
|
+
puts("入力が不適切です。使用できる文字は英数字のみです。");
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
//
|
94
|
+
|
95
|
+
return 1;
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
//
|
100
|
+
|
101
|
+
fputs("再度同じ文字列を入力してください:", stdout);
|
102
|
+
|
103
|
+
fgets(str2, sizeof str2, stdin);
|
104
|
+
|
105
|
+
//
|
106
|
+
|
107
|
+
if (strcmp(str1, str2) == 0) {
|
108
|
+
|
109
|
+
puts("文字列が一致しました。");
|
110
|
+
|
111
|
+
}else{
|
112
|
+
|
113
|
+
puts("文字列が不一致です。");
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
return 0;
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
int check(char *str)
|
126
|
+
|
127
|
+
{
|
128
|
+
|
129
|
+
for (int i = 0; str[i] != '\n'; i++) {
|
130
|
+
|
131
|
+
char ch = str[i];
|
132
|
+
|
133
|
+
if (islower(ch) || isdigit(ch)) {
|
134
|
+
|
135
|
+
continue;
|
136
|
+
|
137
|
+
} else {
|
138
|
+
|
139
|
+
if (isupper(ch)) {
|
140
|
+
|
141
|
+
// 大文字
|
142
|
+
|
143
|
+
return 1;
|
144
|
+
|
145
|
+
} else {
|
146
|
+
|
147
|
+
// その他
|
148
|
+
|
149
|
+
return 2;
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
//
|
158
|
+
|
159
|
+
return 0;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
usr~/test/c %
|
164
|
+
|
165
|
+
```
|