[ C++ ] 객체 포인터와 객체 - string 클래스 find() | C++ 행맨 게임

2021. 4. 13. 12:02·Archive/Develop
728x90
반응형

 

find()

 

찾을 문자열과 어디서부터 찾을 것인지에 대한 인덱스를 인자로 받는 함수이다.

 

 

 

#include <iostream>
#include <string>
using namespace std;

int main() {
	char c = 'A';
	string str = "Apple pear";
	int x = str.find("p", 0);
    int y = str.find("p", 3);
    
    cout << x << endl;
    cout << y << endl;
    
    return 0;
 }

 

 

 

 

 

 

 

find 의 활용

 

 

#include <iostream>
#include <string>
using namespace std;




// 문자열을 입력받아 특정 문자가 몇 개 있는지?
int main() {
	
	char a;
	string str4;
	cout << "영어로 문장을 입력하세여 >> ";
	getline(cin, str4);
	cout << "찾을 문자를 입력하세여 >> ";
	cin >> a;
	int startindex = 0;
	int x = 0;
	int count = 0;
	while (true) {
		x = str4.find(a, startindex);
		if (x != -1) {
			startindex=x+1;
			++count;
		}
		else {
			break;
		}
	}
	cout << endl << "'" << a << "' 문자는 " << count << "개 있습니다.";
}

 

 

 

 

 

 

 

find 의 활용 - 행맨게임(?)

 

 

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

// 임의의 문자 알아맞추기 게임
int main() {
	srand(time(NULL));
	char ch;
	string list[] = {
		"apple", "bear", "banana", "C++", "hohoho"
	};
	int r = rand() % 5;
	string str = list[r];
	cout << "문자를 맞춰봐! >> ";
	string res(str.length(), '_');
	cout << res << endl;
	int x = 0;
	int startindex;
	while (true) {
		startindex = 0;
		cout << "문자를 입력 : ";
		cin >> ch;
		while (true) {
			x = str.find(ch, startindex);
			if (x != -1) {
				res[x] = ch;
				startindex=x+1;
			}
			else {
				break;
			}
		}
		cout << res << endl;
		if (str == res) {
			cout << "성공" << endl;
			break;
		}
		
	}
}

 

 

 

 

728x90
반응형

'Archive > Develop' 카테고리의 다른 글

[ Leetcode ] 125번 Valid Palindrome 풀이  (0) 2021.04.15
코테를 위한 준비 과정(순서)  (0) 2021.04.15
[ Oracle ] 오라클 서브쿼리 예제  (0) 2021.04.12
[ Docker ] Docker Django Container 만들기  (0) 2021.04.08
[ Docker ] Nginx 컨테이너 생성하기  (0) 2021.04.08
'Archive/Develop' 카테고리의 다른 글
  • [ Leetcode ] 125번 Valid Palindrome 풀이
  • 코테를 위한 준비 과정(순서)
  • [ Oracle ] 오라클 서브쿼리 예제
  • [ Docker ] Docker Django Container 만들기
코뮤(commu)
코뮤(commu)
코딩으로 커뮤니케이션하는 코뮤입니다 😎
  • 코뮤(commu)
    코뮤(COMMU)
    코뮤(commu)
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Archive
        • Hacking
        • Develop
        • ETC
      • Algorithm
      • DB&Infra
      • ETC
      • Node
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • IT지식보따리
    • IT가 맛있다
    • IT 천재
  • 공지사항

    • 배고픕니다
  • 인기 글

  • 태그

    Django
    javascript
    파이썬 백준
    Codeup
    파이썬 기초
    장고
    파이썬 문제
    카카오 100일 프로젝트
    파이썬 알고리즘
    백준
    코드업 기초
    oracle db
    백준 파이썬
    자바스크립트 API
    코드업 파이썬 기초 100제
    비박스
    Git
    Python
    코드업
    오라클
    Oracle
    보안뉴스
    docker
    파이썬 기초 문제
    자바스크립트
    백준 풀이
    자바스크립트 객체
    파이썬
    백준 문제풀이
    C++
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
코뮤(commu)
[ C++ ] 객체 포인터와 객체 - string 클래스 find() | C++ 행맨 게임
상단으로

티스토리툴바