Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "mystring.h"
  4.  
  5. char* myreplaceall(char* str, char* suf1, char* suf2);
  6. char* input_str();
  7.  
  8.  
  9. int main() {
  10. printf("Input orig string:\n");
  11. char* message = (char*) input_str();
  12. printf("Input suffix to change:\n");
  13. char* suf1 = (char*)input_str();
  14. printf("Input suffix to change for:\n");
  15. char* suf2 = (char*)input_str();
  16.  
  17. message = myreplaceall(message, suf1, suf2);
  18.  
  19. printf("\nResult:\n");
  20. printf(message);
  21. }
  22.  
  23. char* input_str() {
  24. int c;
  25. char* string = malloc(sizeof(char));
  26. string[0] = '\0';
  27. int i = 0;
  28. while ( (c = getchar()) != '\n')
  29. {
  30. string = realloc(string, (i + 2) * sizeof(char)); //reallocating memory
  31. string[i] = (char)c; //type casting `int` to `char`
  32. string[i + 1] = '\0'; //inserting null character at the end
  33. i++;
  34. }
  35. return string;
  36. }
  37.  
  38. char* myreplaceall(char* str, char* suf1, char* suf2) {
  39. char* res = (char*)malloc(sizeof(char*));
  40. res[0] = '\0';
  41. int i = 0; int i2 = 0; int j = 0;
  42. while (i < strlen(str)) {
  43. while ((str[i + j] == suf1[0 + j]) & (j != strlen(suf1))) j++;
  44. if (j == strlen(suf1)) {
  45. stradd(&res, suf2);
  46. i = i + strlen(suf1);
  47. i2 = i2 + strlen(suf2);
  48. }
  49. else {
  50. char* c = malloc(sizeof(char) * 2);
  51. c[1] = '\0';
  52. c[0] = str[i];
  53. stradd(&res, c);
  54. i++; i2++;
  55. }
  56. j = 0;
  57. }
  58. res[i2] = '\0';
  59. return res;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement