Guest User

Untitled

a guest
Jun 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. char *charReplace(char search, char replace, char *string)
  2. {
  3. int j = 0 ;
  4. char *newChar = new char[strlen(string)];
  5. memset(newChar,0,strlen(string)) ;
  6. for (unsigned int i=0;i<strlen(string);i++)
  7. {
  8. if (replace == NULL)
  9. {
  10. if (string[0] != search)
  11. {
  12. if (string[strlen(string)-1] != search)
  13. {
  14. if (string[i] != search)
  15. {
  16. newChar[j] = string[i] ;
  17. j++ ;
  18. }
  19. else
  20. {
  21. if (j+1 == strlen(string))
  22. {
  23. newChar[j] = string[i+1] ;
  24. j++ ;
  25. }
  26. }
  27. }
  28. else
  29. {
  30. if (j+1 == (strlen(string)-1)) break ;
  31. else
  32. {
  33. newChar[j] = string[i] ;
  34. j++ ;
  35. }
  36. }
  37. }
  38. else
  39. {
  40. if (i+1 == strlen(string)) break ;
  41. else
  42. {
  43. newChar[j] = string[i+1] ;
  44. j++ ;
  45. }
  46. }
  47. }
  48. else
  49. {
  50. if (string[0] != search)
  51. {
  52. if (string[strlen(string)-1] != search)
  53. {
  54. if (string[i] != search)
  55. {
  56. newChar[i] = string[i] ;
  57. if (i+1 == strlen(string))
  58. {
  59. newChar[i] = string[i] ;
  60. newChar[i+1] = 0 ;
  61. }
  62. }
  63. else
  64. {
  65. if (i+1 == strlen(string))
  66. {
  67. newChar[i] = string[i] ;
  68. newChar[i+1] = 0 ;
  69. }
  70. else newChar[i] = replace ;
  71. }
  72. }
  73. else
  74. {
  75. if (i+1 == strlen(string))
  76. {
  77. newChar[i] = replace ;
  78. newChar[i+1] = 0 ;
  79. }
  80. else
  81. {
  82. if (string[i] == search)
  83. newChar[i] = replace ;
  84. else newChar[i] = string[i] ;
  85. }
  86. }
  87. }
  88. else
  89. {
  90. if (string[i] == search)
  91. newChar[i] = replace ;
  92. else
  93. {
  94. if (i+1 == (strlen(string)-1))
  95. {
  96. newChar[i] = string[i] ;
  97. newChar[i+1] = 0 ;
  98. }
  99. else
  100. {
  101. newChar[i] = string[i] ;
  102. if (i+1 == (strlen(string)))
  103. newChar[i+1] = 0 ;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. return newChar ;
  110. }
Add Comment
Please, Sign In to add comment