목록substr (4)
N
https://programmers.co.kr/learn/challenges 코딩테스트 연습 기초부터 차근차근, 직접 코드를 작성해 보세요. programmers.co.kr 주어진 문자열에서 숫자가 영단어로 이뤄진 경우 숫자로 바꿔 리턴하면 된다. for문을 통해 s의 문자를 확인한다. zero, one, eight, nine 처럼 영단어의 시작이 다른 숫자와 겹치지 않기 때문에 해당하는 숫자(문자열로 저장)를 answer에 저장한다. 그리고 단어의 길이 - 1을 i에 더해 다음 문자를 읽도록 한다. 만약 two, three, four, five와 같이 중복되는 경우 substr()함수를 이용해 한번 더 검사하여 해당하는 숫자를 저장한다. 마찬가지로 i에 단어의 길이 - 1을 더한다. 리턴값은 int형이..
28. Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Clarification: What should we return when needle is an empty string? This is a great question to ask during an interview. For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's ..
swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14P0c6AAUCFAYi&categoryId=AV14P0c6AAUCFAYi&categoryType=CODE SW Expert Academy SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요! swexpertacademy.com 10번의 test case가 주어진다. 테스트 케이스 번호, 찾으려 하는 단어, 주어지는 string을 각각 int형 n, string형 search, string형 s 변수에 넣어 주도록 한다. size는 search의 크기를 나타내고, substr()함수를 이용할 때 search의 크기만큼 자르기 위해 사용한다. s의 크기 만큼..

문자열 중 가장 간단한 문제..! 우선 s의 size가 짝수인지 검사를 해준다. 짝수이면 substr을 이용해 가운데 두글자를 반환해주고, 홀수도 마찬가지로 substr을 이용해 가운데 한글자만 반환해준다. #include #include #include using namespace std; string solution(string s) { string answer = ""; if(s.size() % 2 == 0) answer = s.substr(s.size() / 2 - 1, 2); else answer = s.substr(s.size() / 2, 1); return answer; }