Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

8 - Textové řetězce 1/2

$ echo "Pastrnak dal proti Floride opet gol." > pasta.in
$ gcc string.c
$ ./a.out < pasta.in

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
 
void replace_char (char *dest, char a, char b)
{
        int L = strlen (dest);
 
        for (int i = 0; i < L; i++)
                if (*(dest+i) == a) *(dest+i) = b;
}
 
void replace_string (char *dest, char *a, char *b)
{
        int L1 = strlen (dest);
        int L2 = strlen (a);
        int i, j;
 
        for (i = 0; i < L1 - L2; i++)
        {
                for (j = 0; j < L2; j++)
                {
                        if (*(dest + i + j) != *(a+j))
                                break;
                }
                if (j == L2)
                        memcpy (dest + i, b, L2);
        }
}
 
void switch_caps (char *dest)
{
        char *b = dest;
        int L = strlen(dest);
 
        for (int i = 0; i < L; i++, b++)
        {
                if((*b >= 'a' && *b <= 'z') || (*b >= 'A' && *b <= 'Z'))
                {
                        *b ^= 1 << 5;
                }
        }
}
 
int main (void)
{
        char *a = malloc (100);
 
        scanf ("%[^\n]", a);
        printf ("%s\n", a);
 
        replace_char (a, 'o', 'x');
        printf ("%s\n", a);
 
        replace_string (a, "xpet", "XXXX");
        printf ("%s\n", a);
 
        switch_caps (a);
        printf ("%s\n", a);
 
        return 0;
}

courses/b0b99prpa/solutions/lab08.txt · Last modified: 2019/12/05 08:12 by viteks