C++ | STL::string 정리

2023. 10. 31. 19:44·알고리즘/개념 정리
728x90
반응형

* 하나의 sentence로 이루어진 문장에서 단어별로 분리하기

 

#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
    string a = "It is time to study", res;

    int pos, max = INT_MIN;
    while ((pos = a.find(' ')) != string::npos)
    {
        string tmp = a.substr(0, pos);
        int len = tmp.size();
        if (len > max)
        {
            max = len;
            res = tmp;
        }

        cout << tmp << '\n';
        a = a.substr(pos + 1);
    }

    if (a.size() > max)
    {
        max = a.size();
        res = a;
    }

    cout << res << '\n';

    return 0;
}

 

<algorithm> 헤더파일을 통해서 string 안에 소문자, 대문자, 숫자를 구별할 수 있다.

substr(pos, cnt) : pos부터 cnt개수만큼 꺼내온다.

vector와 같이 push_back(), pop_back()로 값을 넣어줄 수 있다.

clear(): string을 비운다.

 

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{
    string a = "Time is 2023Year 10Month";
    cout << a.size() << '\n';

    for (int i = 0; i < a.size(); i++)
    {
        cout << a[i] << " ";
    }
    cout << '\n';

    for (int i = 0; i < a.size(); i++)
    {
        if (isupper(a[i]))
        {
            cout << a[i] << " ";
        }
    }
    cout << '\n';

    for (int i = 0; i < a.size(); i++)
    {
        if (islower(a[i]))
        {
            cout << a[i] << " ";
        }
    }
    cout << '\n';

    for (int i = 0; i < a.size(); i++)
    {
        if (isdigit(a[i]))
        {
            cout << a[i] << " ";
        }
    }
    cout << '\n';

    cout << a.find('Y') << '\n';

    a.push_back('a');
    cout << a << '\n';

    a.pop_back();
    cout << a << '\n';

    a += " 31day";
    cout << a << '\n';

    cout << a.substr(8) << '\n';
    cout << a.substr(8, 4) << '\n';
    a.clear();

    cout << a << '\n';

    return 0;
}
728x90
반응형
저작자표시 비영리 변경금지 (새창열림)

'알고리즘 > 개념 정리' 카테고리의 다른 글

[Java] 2차원 배열 정렬  (0) 2025.06.13
에라토스테네스의 체  (0) 2025.05.04
Java | Dijkstra (최단 경로)  (0) 2025.04.29
알고리즘 대비 코딩테스트용 주요 함수 모음[JAVA]  (0) 2024.01.07
'알고리즘/개념 정리' 카테고리의 다른 글
  • [Java] 2차원 배열 정렬
  • 에라토스테네스의 체
  • Java | Dijkstra (최단 경로)
  • 알고리즘 대비 코딩테스트용 주요 함수 모음[JAVA]
moongi
moongi
프로그래밍 관련 공부를 정리하는 블로그
  • moongi
    By_Me
    moongi
  • 전체
    오늘
    어제
    • 공부 (95)
      • 알고리즘 (75)
        • 구현 (12)
        • 이분 탐색 (1)
        • 누적합 (2)
        • 다이나믹 프로그래밍 (5)
        • 위상 정렬 (2)
        • SCC (1)
        • BFS & DFS (2)
        • 그래프 (2)
        • LCA (2)
        • 세그먼트 트리 (2)
        • 플로이드 워셜 (1)
        • 문자열 (1)
        • 수학 (1)
        • Heap (1)
        • SQL (9)
        • 개념 정리 (5)
        • 예외처리 (1)
      • spring boot (6)
        • jpa (0)
        • querydsl (0)
        • MVC pattern (0)
        • setting (2)
      • 취준 (3)
      • CS (11)
        • 대규모 시스템 설계 (2)
        • 디자인패턴 (2)
        • 데이터베이스 (4)
        • 네트워크 (3)
        • 운영체제 (0)
  • 인기 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
moongi
C++ | STL::string 정리
상단으로

티스토리툴바