// the solution to task 1 of assignment 6.

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


int string_length(char * my_string);


int string_length(char * my_string)
{
  int counter = 0;
  while(1)
  {
    if (my_string[counter] != 0)
    {
      counter++;
    }
    else
    {
      break;
    }
  }

  return counter;
}


char * repeat_letters(char * input, int times)
{
  // convention: length does not include the null character.
  int input_length = string_length(input);
  int output_length = input_length * times;

  char * result = (char *) malloc(sizeof(char) * (output_length+1));
  
  int input_index, j;
  int output_index = 0;
  for (input_index = 0; input_index < input_length; input_index++)
  {
    char current = input[input_index];
    for (j = 0; j < times; j++)
    {
      result[output_index] = current;
      output_index++;
    }
  }
  result[output_length] = 0;
  
  return result;
}


int main()
{
  int repeats;
  char * input = (char *) malloc(sizeof(char) * 1000);

  printf("please enter a string and a number of repetitions:\n");
  scanf("%s %d", input, &repeats);

  if (repeats < 1)
  {
    printf("number of repetitions should be >= 1\n");
    return 0;
  }

  char * new_string = repeat_letters(input, repeats);

  printf("the result string is %s\n", new_string);

  free(input);
  free(new_string);
}


