#1
by bell-chan » 7年前
C言語の課題で、キーボードから文字列を入力し,その文字列の全文字数,小文字の数,大文字の数,数字文字の数,それ以外の文字の数,をそれぞれ表示し、大文字ならば小文字に、小文字ならば大文字に、数字ならばそのまま、その他ならば ‘=’ にする問題です。
(strlen(), islower(), isupper(), isdigit() ,tolower(), toupper() の関数を使用)
--------------------------------------------
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void){
char st[100];
char c;
int i,n,c_l,c_u,c_d,c_f;
printf("文字列を入れてください ");
gets(st);
n = strlen(st);
c_l = c_u = c_d =c_f= 0;
c=st;
for (i = 0; i <= n; i++){
if (islower(st)) {
c_l++;
st = toupper(c);
}
else if (isupper(st)){
c_u++;
st = tolower(c);
}
else if (isdigit(st)) c_d++;
else{
c_f++;
st = '=';
}
}
printf("全文字数 %d 小文字 %d 大文字 %d 数字 %dその他 %d\n", n,c_l, c_u, c_d,c_f);
printf("変換後の文字列は %s\n",st);
return 0;
}
---------------------------------
文字列を入れてください Abc 123 !"# Def
全文字数 15 小文字 4 大文字 2 数字 3その他 7
変換後の文字列は aAA=123=====aAA=
続行するには何かキーを押してください . . .
---------------------------------
となり、本来なら、その他が6で文字列は「aBC=123=====dEF」になるはずなのに上手くいきません。どこが間違っているのか教えてください。
C言語の課題で、キーボードから文字列を入力し,その文字列の全文字数,小文字の数,大文字の数,数字文字の数,それ以外の文字の数,をそれぞれ表示し、大文字ならば小文字に、小文字ならば大文字に、数字ならばそのまま、その他ならば ‘=’ にする問題です。
(strlen(), islower(), isupper(), isdigit() ,tolower(), toupper() の関数を使用)
--------------------------------------------
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void){
char st[100];
char c;
int i,n,c_l,c_u,c_d,c_f;
printf("文字列を入れてください ");
gets(st);
n = strlen(st);
c_l = c_u = c_d =c_f= 0;
c=st[i];
for (i = 0; i <= n; i++){
if (islower(st[i])) {
c_l++;
st[i] = toupper(c);
}
else if (isupper(st[i])){
c_u++;
st[i] = tolower(c);
}
else if (isdigit(st[i])) c_d++;
else{
c_f++;
st[i] = '=';
}
}
printf("全文字数 %d 小文字 %d 大文字 %d 数字 %dその他 %d\n", n,c_l, c_u, c_d,c_f);
printf("変換後の文字列は %s\n",st);
return 0;
}
---------------------------------
文字列を入れてください Abc 123 !"# Def
全文字数 15 小文字 4 大文字 2 数字 3その他 7
変換後の文字列は aAA=123=====aAA=
続行するには何かキーを押してください . . .
---------------------------------
となり、本来なら、その他が6で文字列は「aBC=123=====dEF」になるはずなのに上手くいきません。どこが間違っているのか教えてください。