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/

        <0 - az első kisebb, mint a második
        ==0 - ha egyenlőek
        >0 - az első nagyobb, mint a második
    Pl. ha elválasztójelek a szóköz és vessző,
        akkor az: "indul a gorog aludni, de nem er oda"
        mondat tokenjei:
            indul
            a
            gorog
            aludni
            de
            nem
            er
            oda

Példaprogramok, feladatok

Példa: isalpha és kézi implementációja

 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 <cctype> // ctype.h
using namespace std;

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

    if (isalpha(c))
        cout << "alpha" << endl;
    else
        cout << "nem" << endl;


    if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
        cout << "alpha kezzel is" << endl;
    else
        cout << "nem" << endl;

    return 0;
}

Példa: toupper és kézi implmentációja

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

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

    cout << (char) toupper(c) << endl;

    char masik;
    if ('a' <= c && c <= 'z')
        masik = 'A' + (c - 'a');
    else
        masik = c;
    cout << masik << endl;

    return 0;
}

Példa: strlen

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

int main()
{
    char s[100];
    cin.getline(s,100);

    cout << strlen(s) << endl;
        // 0  ... strlen(s)-1  strlen(s)
        //'h' ... 'o'          '\0'

    cout << strlen("hello") << endl;

    return 0;
}

Példa: strcpy

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

int main()
{
    char str1[100] = "Sample string";
    char str2[100];
    char str3[100];

           //hova,honnan
    strcpy(str2,  str1);
    strcpy(str3,  "copy successful");

    cout << str1 << endl;
    cout << str2 << endl;
    cout << str3 << endl;

    return 0;
}

Példa: strcpy és eltolások

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

int main()
{
    char str1[100] = "sample string";
    char str2[100];

    strcpy(str2, str1+3);
    cout << str2 << endl; // ple string

    strcpy(str2+2, str1);
    cout << str2 << endl; //plsample string

    return 0;
}

HF (fél + pont): Adott egy sor szöveg és egy k természetes szám. Töröljük a szöveg k. betűjét strcpy felhasználásával.

 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()
{

    // k. betű törlése
    char str[100] = "babilon";
    int k = 4;  // bablon

    char seged[100];
    strcpy(seged, str+k);
    strcpy(str+k-1, seged);

    // strcpy(str+k-1, str+k); - undefined behaviour ha átfednek

    cout << str << endl; // bablon
    return 0;
}

Példa: 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 str1[100] = "kis";
    char str2[100] = "macska";

    strcat(str1, str2);

    cout << str1 << endl; // kismacska
    return 0;
}

Feladat: készítsünk saját strcat implementációt my_strcat néven!

 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
#include <iostream>
using namespace std;

char *my_strcat(char *mihez, const char *mit)
{
    int i=0;
    while (mihez[i]!='\0')
        i++;
    int j=0;
    while (mit[j]!='\0'){
        mihez[i]=mit[j];
        i++; j++;
    }
    mihez[i]='\0';
    return mihez;
}

int main()
{
    char a[100] = "hello, ";
    char b[100] = "Lajos";

    my_strcat(a, b);

    cout << a << endl; // hello, Lajos

    return 0;
}

Példa: strcmp

 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
#include <iostream>
#include <cstring>
using namespace std;


int main()
{
    char a[100];
    char b[100];

    cin.getline(a, 100);
    cin.getline(b, 100);

    int r = strcmp(a,b);

    if (r == 0)
        cout << "azonosak" << endl;
    else if (r < 0)
        cout << "az elso van elobb" << endl;
    else
        cout << "a masodik van elobb" << endl;


    return 0;
}

Példa: strchr

 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
#include <iostream>
#include <cstring>
using namespace std;


int main()
{
    char a[100] = "almafamaradvany";

    cout << strchr(a, 'm') << endl; // mafamaradvany
    cout << strrchr(a, 'm') << endl; // maradvany

    cout << strchr(strchr(a, 'm')+1, 'm') << endl; // maradvany

    // az első 'm' pozíciója:
    cout << ( strchr(a, 'm') - a ) << endl; //2

    // adott c karakter magánhangzó-e?
    char c;
    cin >> c;

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


    return 0;
}

Példa: strstr

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


int main()
{
    char a[100] = "almafamaradvanyfaj";
    char b[100] = "fa";

    cout << strstr(a, b) - a << endl; // 4

    return 0;
}

Feladat: Olvassunk be egy sort, majd egy string-darabot. Számoljuk meg, hogy az adott string-darab hányszor fordul elő benne.

    Pl. bemenet:
            almafamaradvanyfaj
            fa
        kimenet:
            2

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

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

    cin.getline(s, 100);
    cin.getline(mit, 100);

    int db = 0;
    char *p = strstr(s, mit);

    while (p != NULL) {
        db++;
        p = strstr(p+1, mit);
    }

    cout << db << endl;
    return 0;
}

Példa: strncpy

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

int main()
{
    char a[100] = "xxxxxxxxxxxxxxxxxxxxx";
    char b[100] = "abcdef";

    strncpy(a, b, 3);
    cout << a << endl; // abcxxx....x

    strncpy(a, b, 20);
    cout << a << endl; // abcdef

    strcpy(a, "xxxxxxxxxxxxxxxxxxxxxxxx");

    strncpy(a, b, 4);
    a[4] = '\0';
    cout << a << endl; // abcd

    return 0;
}

Példa: strncmp

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

int main()
{
    char a[100] = "abcxefg";
    char b[100] = "abcdef";

    cout << strcmp(a, b) << endl; // > 0
    cout << strncmp(a, b, 3) << endl; // 0
    cout << strncmp(a, b, 4) << endl; // > 0
    cout << strncmp(a, b, 200) << endl; // > 0

    return 0;
}

Példa: strtok

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

int main()
{
    char s[100];
    cin.getline(s, 100);

    char *p = strtok(s, " ,.");

    while (p != NULL) {
        cout << p << endl;

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

    return 0;
}

HF. (+pont)
Bonstuk fel egy mondatot szavakra (elválasztójelek: ' ', ',', '.') strtok használata nélkül!

HF (+pont)
Írjuk saját strtok függvényt my_strtok néven! (kell hozzá egy globális változó)

Gyakorlatok: https://www.pbinfo.ro/?pagina=itemi-evaluare-lista&disciplina=0&clasa=10&tag=10&subtag=-1&eticheta=0