반응형
char a[100];
int res=0, cnt=0, i;
scanf("%s", &a);
for(i=0; a[i]!='\0'; i++){
	if(a[i] >=48 &&a[i]<=57){
		res = res*10 + (a[i]-48);
	}
}

char array를 for문을 통해 돌 때

 

for문에서 strlen()을 쓰지 않아도

 

string의 null문자인 '\0'을 활용해서 for문을 끝내 줄 수 있다.

 

 

반응형

'C++' 카테고리의 다른 글

ios::sync_with_stdio(false), cin.tie(0) 쓰는 이유, 백준 시간초과 해결  (0) 2022.01.24
C++ map STL 사용법  (0) 2022.01.24
C++ 반올림  (0) 2022.01.10
C++ 2차원 벡터 선언  (0) 2022.01.10
C++ 한 줄 읽어오기 (C++ parsing)  (0) 2021.12.23

+ Recent posts