Gyakorlatok stringekkel

Gyakorlatok beépített függvényekkel

Mit írnak ki az alábbi programok és miért?

1. strlen:

 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 s[100] = "hello";

    cout << s << endl; // hello
    cout << strlen(s) << endl; // 5

    //cout << strlen(s[2]) << endl; // error lenne

    cout << strlen(s+2) << endl; // 3
    return 0;
}

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

int main()
{
    char s1[100] = "aaaaa";
    char s2[100] = "bbb";

    strcpy(s1, s2);
    cout << s1 << endl; // bbb

    strcpy(s1, "abcdef");
    strcpy(s2, "123456");

    strcpy(s1, s2+3);
    cout << s1 << endl; // 456

    strcpy(s1, "abcdef");
    strcpy(s2, "123456");

    strcpy(s1+4, s2+2);
    cout << s1 << endl; // abcd3456
    cout << s2 << endl; // 123456
    
    return 0;
}

3. strcat:

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

int main()
{
    char s1[100] = "abcd";
    char s2[100] = "123";

    strcat(s1, s2);
    cout << s1 << endl; // abcd123
    cout << s2 << endl; // 123

    strcpy(s1, "abcd");
    strcpy(s2, "123");

    strcat(s1+2, s2);
    cout << s1 << endl; // abcd123

    strcpy(s1, "abcd");
    strcpy(s2, "123");

    strcat(s1, s2+2);
    cout << s1 << endl; // abcd3

    return 0;
}

4. strcmp:

 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()
{
    // strcmp:
    char s1[100] = "almaslepeny";
    char s2[100] = "barack";

    cout << strcmp(s1, s2) << endl; // <0
    cout << strcmp(s1+1, s2+3) << endl; //>0
    
    //cout << strcmp(s1+2, s2[3]) << endl; // error lenne
    
    cout << strcmp(s1+1, &s2[3]) << endl; // >0

    return 0;
}

5. strstr, 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()
{
    // strchr, strstr:

    cout << strstr("abcdxbcdxb", "xb") << endl; //xbcdxb
    if (strstr("abcdxbcdxb", "yb") == NULL)
        cout << "nem volt" << endl;

    char s[100] = "abcdxbcdxb";
    cout << strstr(s, "xb") - s << endl; // 4

    cout << strchr("abcdecf", 'c') << endl; //cdecf
    cout << strrchr("abcdecf", 'c') << endl; // cf

    // magánhangzó-teszt:
    char c;
    cin >> c;

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

    return 0;
}

\6. strncpy:

 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 s[100] = "teszteles";
    char prefix[100] = "aaaaaaaaaaaaaaaaare";

    strncpy(prefix, s, 5);
    prefix[5] = '\0'; // mindenképp be kell tenni kézzel

    cout << prefix << endl;

    return 0;
}

Saját strtok implementáció

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

char* HOL_TARTOTT_A_MYSTRTOK = NULL;

char *my_strtok(char* s, char *jelek) {
    if (s != NULL)
        HOL_TARTOTT_A_MYSTRTOK = s;

    char* p = HOL_TARTOTT_A_MYSTRTOK;
    if (p == NULL || *p == '\0') {
        HOL_TARTOTT_A_MYSTRTOK = NULL;
        return NULL;
    }

    // átugorjuk az elválasztójeleket
    while (strchr(jelek, *p) != NULL)
        p++;

    if (*p == '\0') { // nincs több szó
        HOL_TARTOTT_A_MYSTRTOK = NULL;
        return NULL;
    }

    // van még token, q-val elmegyünk a végére
    char *q = p+1;
    while (*q != '\0' && strchr(jelek, *q) == NULL)
        q++;

    if (*q == '\0')
        HOL_TARTOTT_A_MYSTRTOK = NULL;
    else {
        HOL_TARTOTT_A_MYSTRTOK = q+1;
        *q = '\0';
    }

    return p;
}

int main()
{
    char s[100] = "egy   ketto,. harom";

    // pont úgy kell működjön, mint a beépített strtok
    char* p = my_strtok(s, " ,.");

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

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

    return 0;
}

Feladat érettségi tételből

Egy szövegből ki kellett írni azokat a szavakat (külön sorokba), amelyek valamelyik hangjegynek a nevét tartalmazzák.

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

int main()
{
    char s[101];

    cin.getline ( s, 101 );

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

    bool volt = false;

    char hangjegyek[7][4] = { "DO", "RE", "MI", "FA",
                              "SOL", "LA", "SI" };

    while ( p != NULL )
    {
        bool talalt = false;

        for (int i = 0; i < 7; i++) {
            if (strstr(p, hangjegyek[i]) != NULL)
                talalt = true;
        }

        if (talalt)
        {
            cout << p << endl;
            volt = true;
        }

        p = strtok ( NULL, " " );
    }

    if (!volt)
        cout << "nu exista" << endl;

    return 0;
}