algorithm

[c++] 공백없애기, 올바른 괄호

tonirr 2020. 11. 27. 00:11
  • 공백없애기
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	//freopen("input.txt", "rt", stdin);
	char a[101], b[101];
	int i, p=0;
	gets(a);	// scanf로는 공백을 포함한 문자를 모두 읽을 수 없음 
	for(i=0; a[i]!='\0'; i++){
		if(a[i]!= ' '){
			if(a[i]>=65 && a[i]<=90){
				b[p++]=a[i]+32;
			} else {
				b[p++]=a[i];
			}
		}
	}
	b[p]='\0';
	printf("%s\n", b);
	
	return 0;
}

 

  • 올바른 괄호
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	freopen("input.txt", "rt", stdin);
	char a[101];
	int i, cnt=0;
	scanf("%s", &a);
	for(i=0; a[i]!='\0'; i++){
		if(a[i]=='(') cnt++;
		else if(a[i]==')') cnt--;
		if(cnt<0) break;
	}
	if(cnt==0) printf("YES\n"); 
	else printf("NO\n");
	return 0;
}