C++ STL - cctype

less than 1 minute read

ํ—ค๋”ํŒŒ์ผ cctype (ctype.h)

functions

  • isalnum: ์•ŒํŒŒ๋ฒณ or ์ˆซ์ž๋ฉด true
  • isalpha: ์•ŒํŒŒ๋ฒณ์ด๋ฉด true
  • isblank: space ์ด๊ฑฐ๋‚˜ tab ์ด๋ฉด true
  • isdigit: 0~9์ด๋ฉด true
  • isxdigit: 16์ง„์ˆ˜์ด๋ฉด true
  • islower: ์†Œ๋ฌธ์ž์ด๋ฉด true
  • isupper: ๋Œ€๋ฌธ์ž์ด๋ฉด true
  • isspace: white space๋ฉด true
  • tolower(): ๋Œ€๋ฌธ์ž->์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
  • toupper(): ์†Œ๋ฌธ์ž->๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜

์‚ฌ์šฉ์˜ˆ์‹œ

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

// is+something ํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ if๋ฌธ๊ณผ ๊ฒฐํ•ฉ

int main()
{
    string str1 = "My name is Bokyoung";
    string str2 = "Hello world!";

    for (int i = 0; i < str1.length(); i++) {
        if (islower(str1[i])) {
            str1[i] = toupper(str1[i]);
        }
    }
    cout << str1 << endl;

    for (int i = 0; i < str2.length(); i++) {
        if (isupper(str2[i])) {
            str2[i] = tolower(str2[i]);
        }
    }
    cout << str2 << endl;
}

Categories:

Updated:

Leave a comment