Remove specific word from string
Here given code implementation process.
// Java program
// Remove specific word from string
class MyString
{
// This method are removing given word from given string
// Assuming that given word is single word and it can not containing
// [ - or space]
// String Words are separated by space and - symbol
public String remove_word(String str_data, String word)
{
String auxiliary = "";
String result = "";
//Loop contolling variables
int i = 0, j = 0;
System.out.print("\n Given String \n [" + str_data + "]\n");
//Loop which is iterate given string character elements
for (i = 0; i < str_data.length(); i++)
{
if (str_data.charAt(i) != ' ' && str_data.charAt(i) != '-')
{
//reset variable
auxiliary = "";
//loop, combine string text
for (j = i; j < str_data.length(); j++)
{
//Check that given character is space character or not
if (str_data.charAt(j) != ' ' && str_data.charAt(j) != '-')
{
//When not get space
auxiliary = auxiliary + str_data.charAt(j);
}
else
{
//When find space or - break this loop
break;
}
}
//Check that given word is equal to find word or not
if (!auxiliary.equals(word))
{
//When not equal
result = result + auxiliary;
if (j < str_data.length())
{
result = result + str_data.charAt(j);
}
}
i = j;
}
else
{
//When get space
result = result + str_data.charAt(i);
}
}
return result;
}
public static void main(String[] args)
{
MyString obj = new MyString();
// Simple case
String text = "bug-error This is very diagnosis error please resolve this error";
String word = "error";
text = obj.remove_word(text, word);
System.out.print(" After Remove Word [" + word + "] \n [" + text + "]\n");
text = "an animal, an antidote, a n anything";
word = "an";
text = obj.remove_word(text, word);
System.out.print(" After Remove Word [" + word + "] \n [" + text + "]\n");
}
}
Output
Given String
[bug-error This is very diagnosis error please resolve this error]
After Remove Word [error]
[bug-This is very diagnosis please resolve this ]
Given String
[an animal, an antidote, a n anything]
After Remove Word [an]
[animal, antidote, a n anything]
//Include header file
#include <iostream>
#include<string.h>
using namespace std;
// C++ program
// Remove specific word from string
class MyString
{
public:
// This method are removing given word from given string
// Assuming that given word is single word and it can not containing
// [ - or space]
// String Words are separated by space and - symbol
string remove_word(string str_data, string word)
{
string auxiliary = "";
string result = "";
//Loop contolling variables
int i = 0, j = 0;
cout << "\n Given String \n [" << str_data << "]\n";
//Loop which is iterate given string character elements
for (i = 0; i < str_data.size(); i++)
{
if (str_data[i] != ' ' && str_data[i] != '-')
{
//reset variable
auxiliary = "";
//loop, combine string text
for (j = i; j < str_data.size(); j++)
{
//Check that given character is space character or not
if (str_data[j] != ' ' && str_data[j] != '-')
{
//When not get space
auxiliary = auxiliary + str_data[j];
}
else
{
//When find space or - break this loop
break;
}
}
//Check that given word is equal to find word or not
if (auxiliary.compare(word)!=0)
{
//When not equal
result = result + auxiliary;
if (j < str_data.size())
{
result = result + str_data[j];
}
}
i = j;
}
else
{
//When get space
result = result + str_data[i];
}
}
return result;
}
};
int main()
{
MyString obj = MyString();
// Simple case
string text = "bug-error This is very diagnosis error please resolve this error";
string word = "error";
text = obj.remove_word(text, word);
cout << " After Remove Word [" << word << "] \n [" << text << "]\n";
text = "an animal, an antidote, a n anything";
word = "an";
text = obj.remove_word(text, word);
cout << " After Remove Word [" << word << "] \n [" << text << "]\n";
return 0;
}
Output
Given String
[bug-error This is very diagnosis error please resolve this error]
After Remove Word [error]
[bug-This is very diagnosis please resolve this ]
Given String
[an animal, an antidote, a n anything]
After Remove Word [an]
[animal, antidote, a n anything]
//Include namespace system
using System;
// C# program
// Remove specific word from string
class MyString
{
// This method are removing given word from given string
// Assuming that given word is single word and it can not containing
// [ - or space]
// String Words are separated by space and - symbol
public String remove_word(String str_data, String word)
{
String auxiliary = "";
String result = "";
//Loop contolling variables
int i = 0, j = 0;
Console.Write("\n Given String \n [" + str_data + "]\n");
//Loop which is iterate given string character elements
for (i = 0; i < str_data.Length; i++)
{
if (str_data[i] != ' ' && str_data[i] != '-')
{
//reset variable
auxiliary = "";
//loop, combine string text
for (j = i; j < str_data.Length; j++)
{
//Check that given character is space character or not
if (str_data[j] != ' ' && str_data[j] != '-')
{
//When not get space
auxiliary = auxiliary + str_data[j];
}
else
{
//When find space or - break this loop
break;
}
}
//Check that given word is equal to find word or not
if (!auxiliary.Equals(word))
{
//When not equal
result = result + auxiliary;
if (j < str_data.Length)
{
result = result + str_data[j];
}
}
i = j;
}
else
{
//When get space
result = result + str_data[i];
}
}
return result;
}
public static void Main(String[] args)
{
MyString obj = new MyString();
// Simple case
String text = "bug-error This is very diagnosis error please resolve this error";
String word = "error";
text = obj.remove_word(text, word);
Console.Write(" After Remove Word [" + word + "] \n [" + text + "]\n");
text = "an animal, an antidote, a n anything";
word = "an";
text = obj.remove_word(text, word);
Console.Write(" After Remove Word [" + word + "] \n [" + text + "]\n");
}
}
Output
Given String
[bug-error This is very diagnosis error please resolve this error]
After Remove Word [error]
[bug-This is very diagnosis please resolve this ]
Given String
[an animal, an antidote, a n anything]
After Remove Word [an]
[animal, antidote, a n anything]
<?php
// Php program
// Remove specific word from string
class MyString
{
// This method are removing given word from given string
// Assuming that given word is single word and it can not containing
// [ - or space]
// String Words are separated by space and - symbol
public function remove_word($str_data, $word)
{
$auxiliary = "";
$result = "";
//Loop contolling variables
$i = 0;
$j = 0;
echo "\n Given String \n [". $str_data ."]\n";
//Loop which is iterate given string character elements
for ($i = 0; $i < strlen($str_data); $i++)
{
if ($str_data[$i] != ' ' && $str_data[$i] != '-')
{
//reset variable
$auxiliary = "";
//loop, combine string text
for ($j = $i; $j < strlen($str_data); $j++)
{
//Check that given character is space character or not
if ($str_data[$j] != ' ' && $str_data[$j] != '-')
{
//When not get space
$auxiliary = $auxiliary . $str_data[$j];
}
else
{
//When find space or - break this loop
break;
}
}
//Check that given word is equal to find word or not
if ($word!=$auxiliary)
{
//When not equal
$result = $result . $auxiliary;
if ($j < strlen($str_data))
{
$result = $result . $str_data[$j];
}
}
$i = $j;
}
else
{
//When get space
$result = $result . $str_data[$i];
}
}
return $result;
}
}
function main()
{
$obj = new MyString();
// Simple case
$text = "bug-error This is very diagnosis error please resolve this error";
$word = "error";
$text = $obj->remove_word($text, $word);
echo " After Remove Word [". $word ."] \n [". $text ."]\n";
$text = "an animal, an antidote, a n anything";
$word = "an";
$text = $obj->remove_word($text, $word);
echo " After Remove Word [". $word ."] \n [". $text ."]\n";
}
main();
Output
Given String
[bug-error This is very diagnosis error please resolve this error]
After Remove Word [error]
[bug-This is very diagnosis please resolve this ]
Given String
[an animal, an antidote, a n anything]
After Remove Word [an]
[animal, antidote, a n anything]
// Node Js program
// Remove specific word from string
class MyString
{
// This method are removing given word from given string
// Assuming that given word is single word and it can not containing
// [ - or space]
// String Words are separated by space and - symbol
remove_word(str_data, word)
{
var auxiliary = "";
var result = "";
//Loop contolling variables
var i = 0;
var j = 0;
process.stdout.write("\n Given String \n [" + str_data + "]\n");
//Loop which is iterate given string character elements
for (i = 0; i < str_data.length; i++)
{
if (str_data[i] != ' ' && str_data[i] != '-')
{
//reset variable
auxiliary = "";
//loop, combine string text
for (j = i; j < str_data.length; j++)
{
//Check that given character is space character or not
if (str_data[j] != ' ' && str_data[j] != '-')
{
//When not get space
auxiliary = auxiliary + str_data[j];
}
else
{
//When find space or - break this loop
break;
}
}
//Check that given word is equal to find word or not
if (auxiliary!=word)
{
//When not equal
result = result + auxiliary;
if (j < str_data.length)
{
result = result + str_data[j];
}
}
i = j;
}
else
{
//When get space
result = result + str_data[i];
}
}
return result;
}
}
function main()
{
var obj = new MyString();
// Simple case
var text = "bug-error This is very diagnosis error please resolve this error";
var word = "error";
text = obj.remove_word(text, word);
process.stdout.write(" After Remove Word [" + word + "] \n [" + text + "]\n");
text = "an animal, an antidote, a n anything";
word = "an";
text = obj.remove_word(text, word);
process.stdout.write(" After Remove Word [" + word + "] \n [" + text + "]\n");
}
main();
Output
Given String
[bug-error This is very diagnosis error please resolve this error]
After Remove Word [error]
[bug-This is very diagnosis please resolve this ]
Given String
[an animal, an antidote, a n anything]
After Remove Word [an]
[animal, antidote, a n anything]
# Python 3 program
# Remove specific word from string
class MyString :
# This method are removing given word from given string
# Assuming that given word is single word and it can not containing
# [ - or space]
# String Words are separated by space and - symbol
def remove_word(self, str_data, word) :
auxiliary = ""
result = ""
# Loop contolling variables
i = 0
j = 0
print("\n Given String \n [", str_data ,"]\n", end = "")
# Loop which is iterate given string character elements
i = 0
while (i < len(str_data)) :
if (str_data[i] != ' '
and str_data[i] != '-') :
# reset variable
auxiliary = ""
# loop, combine string text
j = i
while (j < len(str_data)) :
# Check that given character is space character or not
if (str_data[j] != ' '
and str_data[j] != '-') :
# When not get space
auxiliary = auxiliary + str_data[j]
else :
# When find space or - break this loop
break
j += 1
# Check that given word is equal to find word or not
if (auxiliary != word) :
# When not equal
result = result + auxiliary
if (j < len(str_data)) :
result = result + str_data[j]
i = j
else :
# When get space
result = result + str_data[i]
i += 1
return result
def main() :
obj = MyString()
# Simple case
text = "bug-error This is very diagnosis error please resolve this error"
word = "error"
text = obj.remove_word(text, word)
print(" After Remove Word [", word ,"] \n [", text ,"]\n", end = "")
text = "an animal, an antidote, a n anything"
word = "an"
text = obj.remove_word(text, word)
print(" After Remove Word [", word ,"] \n [", text ,"]\n", end = "")
if __name__ == "__main__": main()
Output
Given String
[ bug-error This is very diagnosis error please resolve this error ]
After Remove Word [ error ]
[ bug-This is very diagnosis please resolve this ]
Given String
[ an animal, an antidote, a n anything ]
After Remove Word [ an ]
[ animal, antidote, a n anything ]
# Ruby program
# Remove specific word from string
class MyString
# This method are removing given word from given string
# Assuming that given word is single word and it can not containing
# [ - or space]
# String Words are separated by space and - symbol
def remove_word(str_data, word)
auxiliary = ""
result = ""
# Loop contolling variables
i = 0
j = 0
print("\n Given String \n [", str_data ,"]\n")
# Loop which is iterate given string character elements
i = 0
while (i < str_data.length())
if (str_data[i] != ' ' && str_data[i] != '-')
# reset variable
auxiliary = ""
# loop, combine string text
j = i
while (j < str_data.length())
# Check that given character is space character or not
if (str_data[j] != ' ' && str_data[j] != '-')
# When not get space
auxiliary = auxiliary + str_data[j]
else
# When find space or - break this loop
break
end
j += 1
end
# Check that given word is equal to find word or not
if (auxiliary != word)
# When not equal
result = result + auxiliary
if (j < str_data.length())
result = result + str_data[j]
end
end
i = j
else
# When get space
result = result + str_data[i]
end
i += 1
end
return result
end
end
def main()
obj = MyString.new()
# Simple case
text = "bug-error This is very diagnosis error please resolve this error"
word = "error"
text = obj.remove_word(text, word)
print(" After Remove Word [", word ,"] \n [", text ,"]\n")
text = "an animal, an antidote, a n anything"
word = "an"
text = obj.remove_word(text, word)
print(" After Remove Word [", word ,"] \n [", text ,"]\n")
end
main()
Output
Given String
[bug-error This is very diagnosis error please resolve this error]
After Remove Word [error]
[bug-This is very diagnosis please resolve this ]
Given String
[an animal, an antidote, a n anything]
After Remove Word [an]
[animal, antidote, a n anything]
// Scala program
// Remove specific word from string
import scala.util.control.Breaks._
class MyString
{
// This method are removing given word from given string
// Assuming that given word is single word and it can not containing
// [ - or space]
// String Words are separated by space and - symbol
def remove_word(str_data: String, word: String): String = {
var auxiliary: String = "";
var result: String = "";
//Loop contolling variables
var i: Int = 0;
var j: Int = 0;
print("\n Given String \n [" + str_data + "]\n");
//Loop which is iterate given string character elements
i = 0;
while (i < str_data.length())
{
if (str_data(i) != ' ' && str_data(i) != '-')
{
//reset variable
auxiliary = "";
//loop, combine string text
j = i;
breakable {
while (j < str_data.length())
{
//Check that given character is space character or not
if (str_data(j) != ' ' && str_data(j) != '-')
{
//When not get space
auxiliary = auxiliary + str_data(j);
}
else
{
//When find space or - break this loop
break;
}
j += 1;
}
}
//Check that given word is equal to find word or not
if (!auxiliary.equals(word))
{
//When not equal
result = result + auxiliary;
if (j < str_data.length())
{
result = result + str_data(j);
}
}
i = j;
}
else
{
//When get space
result = result + str_data(i);
}
i += 1;
}
return result;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
// Simple case
var text: String = "bug-error This is very diagnosis error please resolve this error";
var word: String = "error";
text = obj.remove_word(text, word);
print(" After Remove Word [" + word + "] \n [" + text + "]\n");
text = "an animal, an antidote, a n anything";
word = "an";
text = obj.remove_word(text, word);
print(" After Remove Word [" + word + "] \n [" + text + "]\n");
}
}
Output
Given String
[bug-error This is very diagnosis error please resolve this error]
After Remove Word [error]
[bug-This is very diagnosis please resolve this ]
Given String
[an animal, an antidote, a n anything]
After Remove Word [an]
[animal, antidote, a n anything]
// Swift program
// Remove specific word from string
class MyString
{
// This method are removing given word from given string
// Assuming that given word is single word and it can not containing
// [ - or space]
// String Words are separated by space and - symbol
func remove_word(_ text: String, _ word: String) -> String
{
var auxiliary: String = "";
var result: String = "";
let str_data = Array(text);
//Loop contolling variables
var i: Int = 0;
var j: Int = 0;
print("\n Given String \n [", text ,"]\n", terminator: "");
//Loop which is iterate given string character elements
i = 0;
while (i < str_data.count)
{
if (str_data[i] != " " && str_data[i] != "-")
{
//reset variable
auxiliary = "";
//loop, combine string text
j = i;
while (j < str_data.count)
{
//Check that given character is space character or not
if (str_data[j] != " " && str_data[j] != "-")
{
//When not get space
auxiliary = auxiliary + String(str_data[j]);
}
else
{
//When find space or - break this loop
break;
}
j += 1;
}
//Check that given word is equal to find word or not
if (!( auxiliary==word))
{
//When not equal
result = result + auxiliary;
if (j < str_data.count)
{
result = result + String(str_data[j]);
}
}
i = j;
}
else
{
//When get space
result = result + String(str_data[i]);
}
i += 1;
}
return result;
}
}
func main()
{
let obj: MyString = MyString();
// Simple case
var text: String = "bug-error This is very diagnosis error please resolve this error";
var word: String = "error";
text = obj.remove_word(text, word);
print(" After Remove Word [", word ,"] \n [", text ,"]\n", terminator: "");
text = "an animal, an antidote, a n anything";
word = "an";
text = obj.remove_word(text, word);
print(" After Remove Word [", word ,"] \n [", text ,"]\n", terminator: "");
}
main();
Output
Given String
[ bug-error This is very diagnosis error please resolve this error ]
After Remove Word [ error ]
[ bug-This is very diagnosis please resolve this ]
Given String
[ an animal, an antidote, a n anything ]
After Remove Word [ an ]
[ animal, antidote, a n anything ]
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment