Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ Q/A
[1362] 문자열 입력받아 공백세는 프로그램..
오제호 [ohjeho] 2116 읽음    2002-10-17 17:39
안녕하세요
문자열 입력받아서 공백, 콤마, 마침표의 개수를 세는 프로그램인데
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();
}

+ -

관련 글 리스트
1362 문자열 입력받아 공백세는 프로그램.. 오제호 2116 2002/10/17
3648     Re:문자열 입력받아 공백세는 프로그램.. 나오미 1519 2002/10/17
3647     Re:문자열 입력받아 공백세는 프로그램.. 나오미 1481 2002/10/17
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.