IB001 – Seminární skupiny 16, 17, 18 a 19

Reťazce – zdrojový kód

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define STR_MAX 1024

void upperCase(const char *inStr, char* outStr);
void lowerCase(const char *inStr, char* outStr);
void reverse(const char *inStr, char* outStr);
void copy(const char *inStr, char* outStr);
void cat(const char *first, const char* second, char* outStr);
void compare(const char *first, const char* second, char* outStr);

int main()
{
    char first[STR_MAX];
    char second[STR_MAX];
    char result[STR_MAX];
    int action = 0;

    printf("Zadej prvni vstupni retezec\n");
    scanf("%s", first);
    printf("\n");

    printf("Zadej druhy vstupni retezec\n");
    scanf("%s", second);
    printf("\n");

    printf("Zadej, co se ma udelat.\n"
           "1: prevest na velka pismena\n"
           "2: prevest na mala pismena\n"
           "3: vypsat pozadu\n"
           "4: zkopirovat prvni\n"
           "5: doplnit prvni za druhy\n"
           "6: porovnat prvni a druhy\n"
           "> ");
    scanf("%d", &action);

    switch(action)
    {
    case 1:
        upperCase(first, result);
        break;
    case 2:
        lowerCase(first, result);
        break;
    case 3:
        reverse(first, result);
        break;
    case 4:
        copy(first, result);
        break;
    case 5:
        cat(first, second, result);
        break;
    case 6:
        compare(first, second, result);
        break;
    default:
        printf("Neznama operace!");
        return 1;
    }

    printf("\nPrvy vstupny retazec: %s\n", first);
    printf("Druhy vstupny retazec: %s\n", second);

    printf("Vysledny retazec: %s\n", result);

    return 0;
}

void upperCase(const char *inStr, char* outStr)
{
    size_t length = strlen(inStr);
    for (size_t i = 0; i < length; i++)
    {
        outStr[i] = toupper(inStr[i]);
    }
    outStr[length] = '\0';  // Ukoncenie retazca
}

void lowerCase(const char *inStr, char* outStr)
{
    size_t length = strlen(inStr);
    for (size_t i = 0; i < length; i++)
    {
        outStr[i] = tolower(inStr[i]);
    }
    outStr[length] = '\0'; // Ukoncenie retazca
}

void reverse(const char *inStr, char* outStr)
{
    size_t length = strlen(inStr);
    for(size_t i = 0; i < length; i++)
    {
        outStr[i] = inStr[length-i-1];
    }
    outStr[length] = '\0'; // Ukoncenie retazca
}

void copy(const char *inStr, char* outStr)
{
    strcpy(outStr, inStr);
}

void cat(const char *first, const char* second, char* outStr)
{
    strcpy (outStr, first);
    strcat (outStr, second);
}

void compare(const char *first, const char* second, char* outStr)
{
    if (strcmp(first, second) == 0)
        strcpy(outStr, "zhodne");
    else
        strcpy(outStr, "rozdielne");
}