String - beépített függvények

<cctype> - karaktereket feldolgozó függvények

Dokumentáció: https://cplusplus.com/reference/cctype/

<cstring> - karaktersorok feldolgozása

Dokumentáció: https://cplusplus.com/reference/cstring/

Példák

cctype:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cctype>
using namespace std;

int main()
{
    char c;
    cin >> c;

    cout << "isdigit: " << isdigit(c) << endl;
    cout << "isalpha: " << isalpha(c) << endl;
    cout << "isalnum: " << isalnum(c) << endl;
    cout << "isupper: " << isupper(c) << endl;
    cout << "islower: " << islower(c) << endl;

    cout << "toupper: " << (char) toupper(c) << endl;
    cout << "tolower: " << (char) tolower(c) << endl;

    return 0;
}

strlen:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char s[100] = "abcd";

    cout << strlen(s) << endl; // 4
    cout << strlen("abc") << endl; // 3

    cout << strlen(s+2) << endl; // 2

    return 0;
}

strcpy:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char a[100] = "123456789";
    char b[100] = "abc";

    strcpy(a, b);

    cout << a << endl; // abc

    // NEM!!! strcpy(a, a+1);  -- undefined behaviour
    // helyette:
    char c[100];
    strcpy(c, a+1);
    strcpy(a, c);

    cout << a << endl; // bc


    // NEM!  a = "123456789";  helyette:
    strcpy(a, "123456789");
    strcpy(b, "abc");

    strcpy(b+1, a+4);

    cout << b << endl; // a56789
    return 0;
}

strcat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char a[100] = "123456789";
    char b[100] = "abc";

    strcat(a, b);

    cout << a << endl; // 123456789abc

    return 0;
}

strcmp:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    cout << strcmp("alma", "almafa") << endl; // <0
    cout << strcmp("alma", "alma") << endl; // ==0
    cout << strcmp("korte", "alma") << endl; // >0

    return 0;
}

strchr:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char s[100] = "abcdcba";

    char c;
    cout << "tipp: ";
    cin >> c;

    char *p = strchr(s, c);

    if (p == NULL)
        cout << "nincs benne" << endl;
    else
        cout << "benne van a(z) "
             << p - s << " indexen" << endl;

    return 0;
}

Alkalmazás: adott karakter maganhangzó-e?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char c;
    cout << "c = ";
    cin >> c;

    if (strchr("aeiouAEIOU", c) != NULL)
        cout << "maganhangzo" << endl;
    else
        cout << "nem" << endl;

    return 0;
}

strstr:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char s[100] = "kamarazenekar";

    char k[10];
    cout << "k: ";
    cin >> k;

    char *p = strstr(s, k);

    if (p == NULL)
        cout << "nincs meg benne" << endl;
    else
        cout << "megvan, eloszor a(z) "
             << p-s << " indexen" << endl;

    return 0;
}

Minden előfordulás megkeresése:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char s[100];
    char k[100];

    cout << "miben keresunk: ";
    cin.getline(s, 100);

    cout << "mit keresunk: ";
    cin.getline(k, 100);

    int honnan = 0;
    bool megy = true;

    while (megy) {
        char *p = strstr(s+honnan, k);
        if (p == NULL)
            megy = false;
        else {
            cout << "talalt a(z) "
                 << p-s << " indexen" << endl;
            honnan = (p - s) + 1;
        }
    }

    return 0;
}

Ugyanaz másképp (nem eltolást tárolunk, hanem egyenesen memóriacímet):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char s[100];
    char k[100];

    cout << "miben keresunk: ";
    cin.getline(s, 100);

    cout << "mit keresunk: ";
    cin.getline(k, 100);

    char *honnan = s;
    bool megy = true;

    while (megy) {
        char *p = strstr(honnan, k);
        if (p == NULL)
            megy = false;
        else {
            cout << "talalt a(z) "
                 << p-s << " indexen" << endl;
            honnan = p + 1;
        }
    }

    return 0;
}

strncpy:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char s[100] = "almafa";
    int k = 3;

    char masik[100];

    strncpy(masik, s, k);
    // masik: 'a' 'l' 'm' ? ? ?

    masik[k] = '\0';
    // masik: 'a' 'l' 'm' '\0' ? ?

    return 0;
}

strtok:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstring>
using namespace std;

int main ()
{
    char str[] ="- This, a sample string.";

    char *p = strtok (str, " ,.-");
    while (p != NULL)
    {
        cout << strlen(p) << ": " << p << endl;

        p = strtok (NULL, " ,.-");
    }

    cout << str << endl;

    return 0;
}

Gyakorlat

Adott egy max 200 karakteres szöveg, benne kisbetűkből álló szavak egy-egy szóközzel elválasztva. Írjuk ki:

  1. hány szóból áll a szöveg?
  2. mennyi az átlagos szóhossz?
  3. hány olyan szó van, amiben több a magánhangzó, mint a mássalhangzó?
  4. a leghosszabb szót (egyenlőség esetén a lexikografikus sorrend szerinti elsőt)!
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstring>
using namespace std;

int main ()
{
    char str[201];
    cin.getline(str, 201);

    int hany_szo = 0;
    int hosszok_ossz = 0;

    int hany_c = 0;

    int maxhossz = 0;
    char max_szo[201];

    char *p = strtok(str, " ");

    while (p != NULL) {

        // HF!
        int hossz = strlen(p);
        hany_szo++;
        hosszok_ossz += hossz;

        int mgh = 0, msh = 0;
        for (int i = 0; p[i] != '\0'; i++) {
            if (strchr("aeiou", p[i]) != NULL)
                mgh++;
            else
                msh++;
        }
        if (mgh > msh)
            hany_c++;


        if (hossz > maxhossz) {
            maxhossz = hossz;
            strcpy(max_szo, p);
        }
        else if (hossz == maxhossz
                && strcmp(p, max_szo) < 0)
        {
            strcpy(max_szo, p);
        }

        p = strtok(NULL, " ");
    }

    cout << "szavak szama: " << hany_szo << endl;
    cout << "atlagos hossz: "
         << (double)hosszok_ossz / hany_szo << endl;

    cout << "c) " << hany_c << endl;

    cout << "maximalis hossz: " << maxhossz << endl;
    cout << max_szo << endl;

    return 0;
}

A d) alpont másképp: a szó helyett csak az (str-beli) kezdőcímét tároljuk.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstring>
using namespace std;

int main ()
{
    char str[201];
    cin.getline(str, 201);

    int hany_szo = 0;
    int hosszok_ossz = 0;

    int hany_c = 0;

    int maxhossz = 0;
    char *max_szo;

    char *p = strtok(str, " ");

    while (p != NULL) {

        // HF!
        int hossz = strlen(p);
        hany_szo++;
        hosszok_ossz += hossz;

        int mgh = 0, msh = 0;
        for (int i = 0; p[i] != '\0'; i++) {
            if (strchr("aeiou", p[i]) != NULL)
                mgh++;
            else
                msh++;
        }
        if (mgh > msh)
            hany_c++;


        if (hossz > maxhossz) {
            maxhossz = hossz;
            max_szo = p;
        }
        else if (hossz == maxhossz
                && strcmp(p, max_szo) < 0)
        {
            max_szo = p;
        }

        p = strtok(NULL, " ");
    }

    cout << "szavak szama: " << hany_szo << endl;
    cout << "atlagos hossz: "
         << (double)hosszok_ossz / hany_szo << endl;

    cout << "c) " << hany_c << endl;

    cout << "maximalis hossz: " << maxhossz << endl;
    cout << max_szo << endl;

    return 0;
}