fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef struct replace
  5. {
  6. const char *from, *to;
  7. } replace;
  8.  
  9. char* strreplace(char *str, const replace *replaces)
  10. {
  11. char *s = str;
  12. while (*s)
  13. {
  14. const replace *r = replaces;
  15. while (r->from)
  16. {
  17. int len_from = strlen(r->from);
  18. if (strncmp(s, r->from, len_from) == 0)
  19. {
  20. int len_to = strlen(r->to);
  21. memmove(s+len_to, s+len_from, strlen(s+len_from)+1);
  22. memcpy(s, r->to, len_to);
  23. s += len_to - 1;
  24. break;
  25. }
  26.  
  27. ++r;
  28. }
  29.  
  30. ++s;
  31. }
  32.  
  33. return str;
  34. }
  35.  
  36. int main(void) {
  37. char string3[200] = "Hallo, diese Nachricht wird ersetzt. Dieser Quelltext gehoert zu den Systemen.";
  38. replace replaces[] =
  39. {
  40. { "en", "x"},
  41. { "x", "en"},
  42. { "er", "q"},
  43. { "q", "er"},
  44. { "y", "ch"},
  45. { "ch", "y"},
  46. { "a", "o"},
  47. { "o", "a"},
  48. { NULL, NULL }
  49. };
  50.  
  51. printf("Vorher: %s\n", string3);
  52.  
  53. printf("Nachher: %s\n", strreplace(string3, replaces));
  54.  
  55. return 0;
  56. }
  57.  
  58.  
  59.  
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
Vorher: Hallo, diese Nachricht wird ersetzt. Dieser Quelltext gehoert zu den Systemen.
Nachher: Holla, diese Noyriyt wird qsetzt. Diesq Quellteent gehaqt zu dx Schstemx.