Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6.  
  7. void replaceSubstring(char *str, char *substr);
  8. int main()
  9. {
  10. int doloop = 1;
  11. char *str = (char *)(malloc(100));
  12. if (str == NULL)
  13. {
  14. printf("No memory allocated");
  15. return;
  16. }
  17. char *substr = (char *)(malloc(100));
  18. if (substr == NULL)
  19. {
  20. printf("No memory allocated");
  21. return;
  22. }
  23. // we gets the text and substring, then we check if the substring is in the text the we change the letters from small to capital .
  24. while (doloop)
  25. {
  26. printf("Enter Text: ");
  27. gets_s(str, 100);
  28. //empty text
  29. if (*str == '')
  30. {
  31. printf("Finish!n");
  32. doloop = 0;
  33. return;
  34. }
  35. printf("Enter substring: ");
  36. gets_s(substr, 100);
  37. //empty substring
  38. if (*substr == '')
  39. {
  40. printf("Finish!n");
  41. doloop = 0;
  42. return;
  43. }
  44. if (*substr)
  45. replaceSubstring(str, substr);
  46. puts(str);
  47. }
  48. free(substr);
  49. free(str);
  50. _getch();
  51. return 0;
  52. }
  53. /*Function name: replaceSubstring
  54. Input: char *str, char *substr
  55. Output: upercase letter
  56. Algorithm: we used for loop to check every letter if its english letter, and then we replace from lower case to uper case*/
  57.  
  58. void replaceSubstring(char *str, char *substr)
  59. {
  60. int i, len;
  61. char *p;
  62. p = strstr(str, substr);
  63. len = strlen(substr);
  64. while (p)
  65. {
  66. for (i = 0; i < len; i++)
  67. {
  68. // we check if the letters is small english letters
  69. if (*p <= 'z'&&*p >= 'a')
  70. {
  71. *p = *p - 32; // from small letter to capital letter
  72. p++;
  73. }
  74. //else p++;
  75. }
  76. p = strstr(str, substr);
  77. replaceSubstring(str, substr);
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement