daaaar

Word Search for deletion.

Jul 9th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. // practice.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. void deleteWord(char[], char[]);
  8. int findWordStartIndex(char[], char[]);
  9. int findWordEndIndex(char[], char[]);
  10.  
  11. int main()
  12. {
  13.     char paragraph[1000] = { '\0' };
  14.     char word[50] = { '\0' };
  15.  
  16.     cout << "\nFind word and delete it from paragraph.";
  17.  
  18.     cout << "\nEnter paragraph that terminates with a hashtag character: ";
  19.     cin.getline(paragraph,999,'#');
  20.  
  21.     cout << "\nEnter word to delete from paragraph, terminating with a fullstop: ";
  22.     cin.getline(word,49,'.');
  23.  
  24.     deleteWord(paragraph, word);
  25.  
  26.     return 0;
  27.  
  28. }
  29.  
  30. void deleteWord(char paragraph[], char word[])
  31. {
  32.     int starti, endi;
  33.     starti = findWordStartIndex(paragraph, word);
  34.     if (starti == -1){
  35.         cout << "\nERROR: Word not found!";
  36.     }
  37.     else{
  38.         cout << "\nWord found at index: " << starti;
  39.         endi = findWordEndIndex(paragraph, word);
  40.         cout << "\nWord ends at index: " << endi;
  41.         cout << "\nDeletion Continued...";
  42.     }
  43. }
  44. int findWordStartIndex(char paragraph[], char word[])
  45. {
  46.     bool check = false;
  47.     int i = 0, y = 0,x=0, in = 0;
  48.     while (paragraph[i] != '\0'){
  49.         if (paragraph[i] == ' '){ i++; continue; }
  50.         if (paragraph[i] == word[0]){
  51.             x = i;
  52.             while (word[y]!='\0'){
  53.                 if (paragraph[x] != word[y]){
  54.                     break;
  55.                 }
  56.                 x++;;
  57.                 y++;       
  58.             }
  59.             if (word[y] == '\0'){ check = true; }
  60.         }
  61.         if (check == true){ return i; }
  62.         i++;
  63.         y = 0;
  64.     }
  65.     return -1;
  66. }
  67. int findWordEndIndex(char paragraph[], char word[])
  68. {
  69.     bool check = false;
  70.     int i = 0, y = 0, x = 0, in = 0;
  71.     while (paragraph[i] != '\0'){
  72.         if (paragraph[i] == ' ' || paragraph[i]=='.'){
  73.             i++; continue;
  74.         }
  75.         if (paragraph[i] == word[0]){
  76.             x = i;
  77.             while (word[y] != '\0'){
  78.                 if (paragraph[x] != word[y]){
  79.                     break;
  80.                 }
  81.                 x++;;
  82.                 y++;
  83.             }
  84.             if (word[y] == '\0'){ check = true; }
  85.         }
  86.         if (check == true){ return (x-1); }
  87.         i++;
  88.         y = 0;
  89.     }
  90.     return -1;
  91. }
Add Comment
Please, Sign In to add comment