Asif_Anwar

DS_String(SAJID)

Nov 13th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void subString(char str[], int len, int indx)
  5. {
  6. int i;
  7. char temp[1001];
  8. int j = 0;
  9. int sz = strlen(str);
  10. if(indx > sz) {
  11. printf("Invalid input!\n");
  12. return 0;
  13. }
  14. for(i=indx; i<=indx+len; i++){
  15. temp[j++] = str[i];
  16. if(i==sz) break;
  17. }
  18. temp[j] = '\0';
  19. printf("%s\n", temp);
  20. }
  21.  
  22. int findSubString(char str[], char substr[])
  23. {
  24. int szS = strlen(str);
  25. int szSb = strlen(substr);
  26. int i=0, j=0;
  27. int index = -1;
  28. int cnt = 0;
  29. for( ; i<szS; ) {
  30. cnt = 0;
  31. j = 0;
  32. if(str[i]==substr[j]) {
  33. j = 0;
  34. while(str[i]==substr[j] && i<szS) {
  35. i++;
  36. j++;
  37. cnt++;
  38. }
  39. if(cnt==szSb) {
  40. index = i-szSb;
  41. break;
  42. }
  43. }
  44. else i++;
  45. }
  46. return index;
  47. }
  48. void insert(char str[], char newStr[], int indx)
  49. {
  50. int szS = strlen(str);
  51. int szN = strlen(newStr);
  52. char temp[szS+szN+5];
  53. int i = 0;
  54. for(i = 0; i < indx; i++) {
  55. temp[i] = str[i];
  56. }
  57. int j = 0;
  58. for(i=indx; i<indx+szN; i++) {
  59. temp[i] = newStr[j++];
  60. }
  61. j = indx;
  62. for(i=indx+szN; i<szS+szN; i++) {
  63. temp[i] = str[j++];
  64. }
  65. temp[i] = '\0';
  66. printf("%s\n", temp);
  67. }
  68.  
  69. void deleteStr(char str[], int len, int indx)
  70. {
  71. char temp[1001];
  72. int i;
  73. int sz = strlen(str);
  74. for(i=0; i<indx; i++) {
  75. temp[i] = str[i];
  76. }
  77. int j = i;
  78. for(i=indx+len; i<sz; i++) {
  79. temp[j++] = str[i];
  80. }
  81. temp[j] = '\0';
  82. printf("%s\n", temp);
  83. }
  84.  
  85. void replace(char str[], char strTR[], char strR[])
  86. {
  87. char temp[2002];
  88. int indx = findSubString(str, strR);
  89. if(indx==-1) {
  90. printf("String to be replace is not found!\n");
  91. return;
  92. }
  93. int len = strlen(str);
  94. int lenR = strlen(strR);
  95. int lenTR = strlen(strTR);
  96. int i=0;
  97. for(i=0; i<indx; i++) {
  98. temp[i] = str[i];
  99. }
  100. int j=i;
  101. for(i=0; i<lenTR; i++) {
  102. temp[j++] = strTR[i];
  103. }
  104. for(i=indx+lenR; i<len; i++) {
  105. temp[j++] = str[i];
  106. }
  107. temp[j] = '\0';
  108. printf("%s", temp);
  109. }
  110.  
  111. int main()
  112. {
  113. char str[1001];
  114. int len = 0, indx = 0;
  115.  
  116. //Assignment-1
  117.  
  118. printf("\tAssignment-1:\n\n");
  119.  
  120. printf("Enter a string(within 1000 characters): ");
  121. gets(str);
  122. printf("Enter the index(index staring from 0): ");
  123. scanf("%d", &indx);
  124. printf("Enter the length of the sub-string: ");
  125. scanf("%d", &len);
  126.  
  127. subString(str, len, indx);
  128.  
  129. // Assignment-2
  130. getchar();
  131. printf("\n\tAssignment-2:\n");
  132.  
  133. printf("Enter the sub-string you are looking for: ");
  134. char substr[1001];
  135. gets(substr);
  136. int x = findSubString(str, substr);
  137. if(x==-1) printf("Sub-string not found\n");
  138. else printf("Sub-string found at index %d", x);
  139.  
  140. // Assignment-3
  141. printf("\n\tAssignment-3:\n");
  142.  
  143. char stringToInsert[1001];
  144. printf("Enter the string you want to insert: ");
  145. gets(stringToInsert);
  146. printf("Enter the index you want to insert the new string: ");
  147. scanf("%d", &indx);
  148. insert(str, stringToInsert, indx);
  149.  
  150. // Assignment-4
  151. printf("\n\tAssignment-4:\n");
  152. printf("Enter the index from which you want to delete: ");
  153. scanf("%d", &indx);
  154. printf("Enter the size of sub-string you want to delete: ");
  155. scanf("%d", &len);
  156. deleteStr(str, len, indx);
  157.  
  158. // Assignment-5
  159. printf("\n\tAssignment-5:\n");
  160. getchar();
  161. char strR[1001], strTR[1001];
  162. printf("Enter the sub-string you want to replace with: ");
  163. gets(strTR);
  164. printf("Enter the string you want to replace: ");
  165. gets(strR);
  166. replace(str, strTR, strR);
  167. return 0;
  168. }
  169.  
Advertisement
Add Comment
Please, Sign In to add comment