top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Given a sentence and a word, remove all occurrences of the word in the sentence using C/Java?

0 votes
703 views

Given a sentence and a word, remove all occurrences of the word in the sentence.

For example, removing “is” from the sentence “This is a boy.” becomes “Th a boy.”

posted Oct 17, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Hi find your solution in c++ : https://ideone.com/I1Ic1j
where I used find and replace functions from string and if you want to implement it in java,it will be easy to implement with this solution.

#include <iostream>
#include<string>
using namespace std;

// To execute C++, please define "int main()"
int main() {

   string str="Is it appropriate ans to your problem? : by queryhome",key="ho";
 // cin>>str>>key;
  size_t i = 0;
while (true) {
     i = str.find(key, i);
     if (i== std:: string ::npos) 
     {
        break;
     }
     str.replace(i, key.length(),"");
     i += 1;
}
  cout<<str<<endl;
  return 0;
}
answer Oct 17, 2016 by Shivam Kumar Pandey
without seeing your requirement, I wrote this in c++, if you  are not able to write solution in c/java then comment below
Similar Questions
+3 votes

Implement a code that counts the occurrences of each word in an input file and write the word along with corresponding count in an output file sorted by the words alphabetically.

Sample Input

Gaurav is  a Good Boy
Sita is a Good Girl
Tommy is  a Good Dog
Ram is a Good person

Sample Output

D:\>Java FileWordCount inputFile.txt outputFile.txt
    Boy : 1
    Dog : 1
    Gaurav : 1
    Girl : 1
    Good : 4
    Ram : 1
    Sita : 1
    Tommy : 1
    a : 4
    is : 4
    person : 1
...