Programming contests

50 Programming Exercise for Beginners

January 2, 2019 12:00 AM – December 31, 2019 12:00 AM

Digits

Consider the following header file:

myheader.h

  1. #ifndef _MYHEADER_H
  2. #define _MYHEADER_H 1
  3. void foo(char *, const char *);
  4. #endif /* myheader.h */
download as text file

Write the foo() function declared in myheader.h that takes the address of two strings as parameters. The function should concatenate the digit characters in the second string into a single string and store this string at the memory location pointed to by the first parameter.

Note

Place the function in a file named foo.c and submit this file as a solution to the evaluation system. You can test your solution using the following files. The evaluation system does not necessarily perform the evaluation using these files.

main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "myheader.h"
  5. int main()
  6. {
  7.     char source[100], destination[100];
  8.     while (fgets(source, 100, stdin) != NULL)
  9.     {
  10.         if (source[strlen(source) - 1] == '\n')
  11.             source[strlen(source) - 1] = '\0';
  12.         foo(destination, source);
  13.         printf("%s\n", destination);
  14.     }
  15.     return EXIT_SUCCESS;
  16. }
download as text file

Makefile

  1. SRCS = main.c foo.c
  2. OBJS = $(SRCS:%.c=%.o)
  3. TARGETS = main
  4. .PHONY: clean
  5. all: $(TARGETS)
  6. main: $(OBJS)
  7. $(CC) $(OBJS) -o main
  8. %.o: %.c
  9. $(CC) -Wall -c $< -o $@
  10. clean:
  11. rm -rf $(OBJS) *~ $(TARGETS)
download as text file

Sample Input

  1. A1B2C3
  2. ABC123
  3. ABCDEF
  4. 123456
download as text file

Output for Sample Input

  1. 123
  2. 123
  3. 123456
download as text file

One possible solution is available in: main.c.

Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 03/01/2019