﻿/**
 *   Løsning til eksamen i GrProg (i C), desember 2025, oppgave 1B.
 *
 *   @file     EX_H25_1B.C
 *   @author   Frode Haug, NTNU
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>


struct Lag  {
   char navn[30];
   char stadion[30];
};


char txt1[][30] = { "Arsenal",  "Portsmouth",   "Ipswich",      "Brentford" };
char txt2[][30] = { "Emirates", "Fratton Park", "Portman Road", "Community" };

struct Lag lagene[4];


void H25Funk1(const struct Lag l, const bool s)  {
  printf("%s: %s", l.navn, l.stadion);
  if (s) printf(" Stadium");
}


void H25Funk2(const char ch)  {
  for (int i = 0;  i < 4;  i++)
      if (strchr(txt1[i], ch) != NULL  &&  strchr(txt2[i], ch) != NULL)
         printf("%i ", i);
}


bool H25Funk3(const int n)  {
  return (strlen(lagene[n].navn) < strlen(lagene[n].stadion));
}


char* H25Funk4(const int i, const int j)  {
  char* t = (char*) malloc(40 * sizeof(char));
  strcpy(t, txt1[i]);   strcat(t, " - ");  strcat(t, txt2[j]);
  return t;
}


int main()  {

  for (int i = 0;  i < 4;  i++)
      if (i % 2 == 0) printf("%s  ", txt1[i]);
      else            printf("%s  ", txt2[i]);
  printf("\n");


  for (int i = 3;  i >= 0;  i--)  {
      strcpy(lagene[i].navn,    txt1[3-i]);
      strcpy(lagene[i].stadion, txt2[3-i]);
  }
  H25Funk1(lagene[1], false);  printf(" ... ");
  H25Funk1(lagene[0], true);   printf("\n");

  H25Funk2('o');  printf("\n");

  printf("%i %i\n", H25Funk3(3), H25Funk3(0));

  printf("%s\n", H25Funk4(2, 1));

  return 0;
}



//  Utskrift:  Arsenal  Fratton Park  Ipswich  Community
//             Ipswich: Portman Road ... Brentford: Community Stadium
//             1 3
//             1 0
//             Ipswich - Fratton Park

