-
문자열 concat, 문자열 찾기_220714데일리 codeup/문자열 2022. 7. 14. 15:29
문자열 concat
문자열 추가하기
알파벳으로 이루어진 문자열이 주어지면 뒤에 'Hello'를 붙여서 저장하고 출력하는 프로그램을 작성해보세요.
#include <iostream> #include <string> using namespace std; int main(){ string str; cin >> str; str += "Hello"; cout<<str; return 0; }
문자열 전부 붙이기
정수 n이 주어지고, n개의 줄에 걸쳐 총 n개의 문자열이 주어질 때, 모든 문자열을 붙여 출력하는 프로그램을 작성해보세요.
#include <iostream> #include <string> using namespace std; int main(){ int n; string tmp, str; cin >> n; for(int i=0;i<n;i++){ cin >> tmp; str += tmp; } cout<<str; return 0; }
문자열 나누기
공백과 숫자로만 이루어진 문자열과 그 문자열의 개수가 주어지면 다섯 개의 숫자씩 나누어서 출력하는 프로그램을 작성해보세요.
입력
9 12 34 567 89 23 123 12 312 84
출력
12345 67892 31231 23128 4
#include <iostream> #include <string> using namespace std; int main(){ int n; string tmp, str; cin >> n; for(int i=0;i<n;i++){ cin >> tmp; str += tmp; } for(int i=0;i<str.length();i++){ if(i%5==0 && i!=0) cout<<endl; cout<<str[i]; } return 0; }
두 문자열을 이어붙였을 때
두 문자열 A, B을 이어붙여서 새로운 문자열 A + B , B + A 을 만들었을 때, 두 문자열이 같은지 비교하는 프로그램을 작성해보세요.
#include <iostream> #include <string> using namespace std; int main(){ string str1,str2,str3,str4; cin>> str1 >> str2; str3 = str1 + str2; str4 = str2 + str1; if(str3 == str4) cout<<"true"; else cout<<"false"; return 0; }
문자열 찾기
c++에서는
string
헤더의substr
함수를 이용하여 다음과 같이 직접 문자열끼리 비교해주는 것이 가능 하다.문자열.substr(시작 인덱스, 부분문자열 길이)
string s = "appleabanana"; int length = s.length(); bool exists = false; for (int i = 0; i < length - 1; i++) { if(s.substr(i, 2) == "ab") { exists = true; } }
c++ 에서는
string
헤더의find
함수를 이용하면 부분문자열 포함 여부를 간단하게 알 수 있다.find
함수가 부분문자열을 찾는데 성공하면 (여러번 나타나면 가장 앞선) 부분문자열 첫 문자의index
를 반환하며, 실패한다면string::npos
라는 상수를 반환한다.string s = "appleabanana"; bool exists = false; if (s.find("ab") != string::npos) { exists = true; }
특정 문자의 유무
문자열이 주어지면 문자열 'ee'와 'ab'의 포함여부를 출력하는 프로그램을 작성해보세요.
#include <iostream> #include <string> using namespace std; int main(){ string str; cin >> str; bool ee = false, ab = false; for(int i=0;i<str.length()-1;i++){ if(str[i]=='e'&&str[i+1]=='e') ee = true; if(str[i]=='a'&&str[i+1]=='b') ab = true; } if(ee) cout<<"Yes"<<' '; else cout<<"No"<<' '; if(ab) cout<<"Yes"<<' '; else cout<<"No"<<' '; return 0; }
문자열의 특정 위치 찾기
알파벳으로 이루어진 문자열과 문자가 하나가 주어지면 주어진 문자의 위치를 출력하는 프로그램을 작성해보세요. 단, 첫 번째 문자의 위치는 0, 두 번째의 문자의 위치는 1, ... 이렇게 0번부터 세기 시작합니다.
#include <iostream> #include <string> using namespace std; int main(){ string str; char word; cin >> str>> word; if(str.find(word) != string::npos) cout<<str.find(word); else cout<<"No"; return 0; }
특정 문자의 등장 횟수
문자열이 주어지면 주어진 문자열에 'ee', 'eb'가 각각 몇 번씩 나왔는지를 출력하는 프로그램을 작성해보세요.
#include <iostream> #include <string> using namespace std; int main(){ int n = 0, m = 0; string str; cin >> str; for(int i=0;i<str.length()-1;i++){ if(str[i]=='e'&&str[i+1]=='e') n++; if(str[i]=='e'&&str[i+1]=='b') m++; } cout<<n<<' '<<m; return 0; }
부분문자열 위치 구하기
주어진 입력 문자열에 대하여 목적 문자열이 부분 문자열로 존재하는 경우, 부분 문자열의 시작 인덱스를 출력하는 코드를 작성해보세요. 인덱스는 0부터 시작한다고 가정합니다.
#include <iostream> #include <string> using namespace std; int main(){ string str,obj_str; int num,i; bool is_same = false; cin >> str >> obj_str; for(i=0;i<str.length();i++){ if(str[i]== obj_str[0]){ is_same = true; for(int j=1;j<obj_str.length();j++){ if(str[i+j]!=obj_str[j]){ is_same = false; break; } } } if(is_same == true){ break; } } if(is_same == true) cout<<i; else cout<<-1; return 0; }
부분 문자열의 개수
두 문자열 A와 B가 주어졌을 때, 문자열 B가 문자열 A의 부분 문자열로써 등장하는 횟수를 구하는 프로그램을 작성해보세요. 문자열 B의 길이는 항상 2임을 가정하여도 좋습니다.
#include <iostream> #include <string> using namespace std; int main(){ string str_a,str_b; cin >> str_a >> str_b; int cnt = 0; for(int i=0;i<str_a.length()-1;i++){ if(str_a[i]==str_b[0] && str_a[i+1] == str_b[1]) cnt++; } cout<<cnt; return 0; }
'데일리 codeup > 문자열' 카테고리의 다른 글
문자열 밀기_220716 (0) 2022.07.16 문자 수정, 문자 삭제_220716 (0) 2022.07.16 문자열 순회하기_220714 (0) 2022.07.14 문자열 리스트 관리_220713 (0) 2022.07.13 공백없는 문자열, 공백 있는 문자열_220713 (0) 2022.07.13