fork download
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. void print(char const* string, int count, ...)
  7. {
  8. struct data
  9. {
  10. char name[100];
  11. int value;
  12. };
  13.  
  14. int totalArgs = count;
  15. count*=2;
  16.  
  17. va_list args;
  18. va_start(args, count);
  19.  
  20. struct data * parameters = (struct data*)malloc(totalArgs*sizeof(struct data));
  21. for( int i=0; i<totalArgs; i++)
  22. {
  23. strcpy(parameters[i].name, va_arg(args, char*));
  24. parameters[i].value = va_arg(args, int);
  25. }
  26.  
  27. va_end(args);
  28.  
  29. for( int i=0; string[i]!='\0'; ++i )
  30. {
  31. if( string[i] == '{' )
  32. {
  33. int replaced = 0;
  34. int j;
  35. for( j=i+1; string[j]!='}'; ++j );
  36.  
  37. for( int dataIndex=0; dataIndex<totalArgs; ++dataIndex )
  38. {
  39. if( memcmp(string+i+1, parameters[dataIndex].name, j-i-1) == 0 )
  40. {
  41. printf("%d", parameters[dataIndex].value);
  42. i=j;
  43. replaced = 1;
  44. break;
  45. }
  46. }
  47.  
  48. if( !replaced )
  49. {
  50. while(i<=j)
  51. {
  52. putchar(string[i++]);
  53. }
  54. i--;
  55. }
  56. }
  57. else
  58. {
  59. putchar(string[i]);
  60. }
  61. }
  62.  
  63. free(parameters);
  64. }
  65.  
  66. int main()
  67. {
  68. int value = 123;
  69. print("X={X} value={value} other={other}", 2, "X", 44, "value", value);
  70. }
Success #stdin #stdout 0s 4752KB
stdin
Standard input is empty
stdout
X=44 value=123 other={other}