Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // practice.cpp : Defines the entry point for the console application.
- //
- #include<iostream>
- using namespace std;
- void deleteWord(char[], char[]);
- int findWordStartIndex(char[], char[]);
- int findWordEndIndex(char[], char[]);
- int main()
- {
- char paragraph[1000] = { '\0' };
- char word[50] = { '\0' };
- cout << "\nFind word and delete it from paragraph.";
- cout << "\nEnter paragraph that terminates with a hashtag character: ";
- cin.getline(paragraph,999,'#');
- cout << "\nEnter word to delete from paragraph, terminating with a fullstop: ";
- cin.getline(word,49,'.');
- deleteWord(paragraph, word);
- return 0;
- }
- void deleteWord(char paragraph[], char word[])
- {
- int starti, endi;
- starti = findWordStartIndex(paragraph, word);
- if (starti == -1){
- cout << "\nERROR: Word not found!";
- }
- else{
- cout << "\nWord found at index: " << starti;
- endi = findWordEndIndex(paragraph, word);
- cout << "\nWord ends at index: " << endi;
- cout << "\nDeletion Continued...";
- }
- }
- int findWordStartIndex(char paragraph[], char word[])
- {
- bool check = false;
- int i = 0, y = 0,x=0, in = 0;
- while (paragraph[i] != '\0'){
- if (paragraph[i] == ' '){ i++; continue; }
- if (paragraph[i] == word[0]){
- x = i;
- while (word[y]!='\0'){
- if (paragraph[x] != word[y]){
- break;
- }
- x++;;
- y++;
- }
- if (word[y] == '\0'){ check = true; }
- }
- if (check == true){ return i; }
- i++;
- y = 0;
- }
- return -1;
- }
- int findWordEndIndex(char paragraph[], char word[])
- {
- bool check = false;
- int i = 0, y = 0, x = 0, in = 0;
- while (paragraph[i] != '\0'){
- if (paragraph[i] == ' ' || paragraph[i]=='.'){
- i++; continue;
- }
- if (paragraph[i] == word[0]){
- x = i;
- while (word[y] != '\0'){
- if (paragraph[x] != word[y]){
- break;
- }
- x++;;
- y++;
- }
- if (word[y] == '\0'){ check = true; }
- }
- if (check == true){ return (x-1); }
- i++;
- y = 0;
- }
- return -1;
- }
Add Comment
Please, Sign In to add comment