Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include <string.h>
  4.  
  5.  
  6. typedef struct rule1{
  7. char *a;
  8. char *b;
  9. }ru1;
  10.  
  11. typedef struct rule2 {
  12. char *a;
  13. char *b;
  14. }ru2;
  15.  
  16. int main(){
  17.  
  18. int k = 0;
  19.  
  20. ru1 r1;
  21. r1.a = (char*)malloc(1 * sizeof(char)); r1.a = "A";
  22. r1.b = (char*)malloc(2 * sizeof(char)); r1.b = "AB" ;
  23. ru2 r2;
  24. r2.a = (char*)malloc(1 * sizeof(char)); r2.a = "B";
  25. r2.b = (char*)malloc(1 * sizeof(char)); r2.b = "A";
  26.  
  27.  
  28. char *sentence=(char*)malloc((k+1) * sizeof(char));
  29. sentence = "A";
  30.  
  31. char *nextSentence ;
  32. char*ns;
  33.  
  34. for (int i = 0; i < strlen(sentence);++i)
  35. {
  36. char current = sentence[i];
  37. if (current == r1.a)
  38. {
  39. k++;
  40. strcat(nextSentence, r1.b);
  41. }
  42. else if (current == r2.a)
  43. {
  44. strcat(nextSentence, r2.b);
  45. }
  46. sentence = (char*)realloc(sentence, (k + 1) * sizeof(char));
  47. sentence = nextSentence;
  48. }
  49.  
  50. printf("%s\n", sentence);
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement