|
안녕하세요
문자열 입력받아서 공백, 콤마, 마침표의 개수를 세는 프로그램인데
for문으로 하면 정확한 결과가 나오는데
while문으로 하면 콤마가 없어도 콤마는 1이 출력됩니다.
while문에서 제가 실수한게 뭔지 모르겠습니다.
답변좀 부탁드립니다.
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100];
int i=0;
int cnt_space, cnt_comma, cnt_period;
cnt_space=0;
cnt_comma=0;
cnt_period=0;
printf("Input string : ");
gets(str);
printf("Inputed string : ");
puts(str);
/*
for(; str[i] ;i++ )
{
if(str[i]==' ')
cnt_space++;
else if(str[i]==',')
cnt_comma++;
else if(str[i]=='.')
cnt_period++;
}
*/
while( str[i++] != EOF )
{
if(str[i]==' ')
cnt_space++;
else if(str[i]==',')
cnt_comma++;
else if(str[i]=='.')
cnt_period++;
}
printf(" num of space is %d, num of comma is %d, num of period is %d",cnt_space,cnt_comma,cnt_period);
getch();
}
|