Posted on by Kalkicode
Code String

Replace substring with a another string

Here given code implementation process.

/*
    C++ Program
    Replace substring with another string in a string
*/
#include <iostream>

#include <string.h>

using namespace std;
//Replace substring with a another string
//Here text is given string
//And substring which is modifying of given pattern
string modified(string text, string substring, string pattern)
{
	string result = "";
	bool status = false;
	//loop controlling variables
	int i = 0, j = 0;
	//Display given information
	cout << " Given Text : " << text << endl;
	cout << " Substring : " << substring << endl;
	cout << " Pattern : " << pattern << endl;
	//Loop iterating string(char) elements
	for (i = 0; i < text.length(); ++i)
	{
		//reset status flag
		status = false;
		//check that first character of substring is exist or not in a given text
		if (text[i] == substring[0])
		{
			status = true;
			//Check that text contains substring or not?
			for (j = 1; status == true && j < substring.length() && (i + j) < text.length(); ++j)
			{
				if (text[i + j] != substring[j])
				{
					//When substring characters are not exist
					status = false;
				}
			}
			if (status == true && j < substring.length())
			{
				//When substring portion are not exist
				status = false;
			}
		}
		if (status == true)
		{
			//Add pattern
			result += pattern;
			i += substring.length() - 1;
		}
		else
		{
			//Add single character
			result += text[i];
		}
	}
	return result;
}
// main function
int main()
{
	string text = "super6and4superstarsupersongsupergame";
	string substring = "super";
	string pattern = "good";
	text = modified(text, substring, pattern);
	cout << "\n Result : " << text;
	return 0;
}

Output

 Given Text : super6and4superstarsupersongsupergame
 Substring : super
 Pattern : good

 Result : good6and4goodstargoodsonggoodgame
//Java Program 
//Replace substring with another string in a string
class MyString
{
	// Replace substring with a another String
	// Here text is given string
	// And substring which is modifying of given pattern
	public String modified(String text, String substring, String pattern)
	{
		String result = "";
		boolean status = false;
		//loop controlling variables
		int i = 0, j = 0;
		//Display given information
		System.out.print(" Given Text : " + text + "\n");
		System.out.print(" Given Substring : " + substring + "\n");
		System.out.print(" Given Pattern : " + pattern + "\n");
		//Loop iterating String(char) elements
		for (i = 0; i < text.length(); ++i)
		{
			//reset status flag
			status = false;
			//check that first character of substring is exist or not in a given text
			if (text.charAt(i) == substring.charAt(0))
			{
				status = true;
				//Check that text contains substring or not?
				for (j = 1; status == true && j < substring.length() && (i + j) < text.length(); ++j)
				{
					if (text.charAt(i + j) != substring.charAt(j))
					{
						//When substring characters are not exist
						status = false;
					}
				}
				if (status == true && j < substring.length())
				{
					//When substring portion are not exist
					status = false;
				}
			}
			if (status == true)
			{
				//Add pattern
				result += pattern;
				i += substring.length() - 1;
			}
			else
			{
				//Add single character
				result += text.charAt(i);
			}
		}
		return result;
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		String text = "super6and4superstarsupersongsupergame";
		String substring = "super";
		String pattern = "good";
		text = obj.modified(text, substring, pattern);
		System.out.print(" Result : " + text + "\n");
	}
}

Output

 Given Text : super6and4superstarsupersongsupergame
 Given Substring : super
 Given Pattern : good
 Result : good6and4goodstargoodsonggoodgame
//Include namespace system
using System;
//C# Program 
//Replace substring with another string in a string
public class MyString
{
	// Replace substring with a another String
	// Here text is given string
	// And substring which is modifying of given pattern
	public String modified(String text, String substring, String pattern)
	{
		String result = "";
		Boolean status = false;
		//loop controlling variables
		int i = 0, j = 0;
		//Display given information
		Console.Write(" Given Text : " + text + "\n");
		Console.Write(" Given Substring : " + substring + "\n");
		Console.Write(" Given Pattern : " + pattern + "\n");
		//Loop iterating String(char) elements
		for (i = 0; i < text.Length; ++i)
		{
			//reset status flag
			status = false;
			//check that first character of substring is exist or not in a given text
			if (text[i] == substring[0])
			{
				status = true;
				//Check that text contains substring or not?
				for (j = 1; status == true && j < substring.Length && (i + j) < text.Length; ++j)
				{
					if (text[i + j] != substring[j])
					{
						//When substring characters are not exist
						status = false;
					}
				}
				if (status == true && j < substring.Length)
				{
					//When substring portion are not exist
					status = false;
				}
			}
			if (status == true)
			{
				//Add pattern
				result += pattern;
				i += substring.Length - 1;
			}
			else
			{
				//Add single character
				result += text[i];
			}
		}
		return result;
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		String text = "super6and4superstarsupersongsupergame";
		String substring = "super";
		String pattern = "good";
		text = obj.modified(text, substring, pattern);
		Console.Write(" Result : " + text + "\n");
	}
}

Output

 Given Text : super6and4superstarsupersongsupergame
 Given Substring : super
 Given Pattern : good
 Result : good6and4goodstargoodsonggoodgame
<?php
//Php Program 
//Replace substring with another string in a string
class MyString
{
	// Replace substring with a another String
	// Here text is given string
	// And substring which is modifying of given pattern
	// pattern is other substring
	public	function modified($text, $substring, $pattern)
	{
		$result = "";
		$status = false;
		//loop controlling variables
		$i = 0;
		$j = 0;
		echo " Given Text : ". $text ."\n";
		echo " Given Substring : ". $substring ."\n";
		echo " Given Pattern : ". $pattern ."\n";
		//Loop iterating String(char) elements
		for ($i = 0; $i < strlen($text); ++$i)
		{
			//reset status flag
			$status = false;
			//check that first character of substring is exist or not in a given text
			if ($text[$i] == $substring[0])
			{
				$status = true;
				//Check that text contains substring or not?
				for ($j = 1; $status == true && $j < strlen($substring) && ($i + $j) < strlen($text); ++$j)
				{
					if ($text[$i + $j] != $substring[$j])
					{
						//When substring characters are not exist
						$status = false;
					}
				}
				if ($status == true && $j < strlen($substring))
				{
					//When substring portion are not exist
					$status = false;
				}
			}
			if ($status == true)
			{
				//Add pattern
				$result .= $pattern;
				$i += strlen($substring) - 1;
			}
			else
			{
				//Add single character
				$result .= $text[$i];
			}
		}
		return $result;
	}
}

function main()
{
	$obj = new MyString();
	$text = "super6and4superstarsupersongsupergame";
	$substring = "super";
	$pattern = "good";
	$text = $obj->modified($text, $substring, $pattern);
	echo " Result : ". $text ."\n";
}
main();

Output

 Given Text : super6and4superstarsupersongsupergame
 Given Substring : super
 Given Pattern : good
 Result : good6and4goodstargoodsonggoodgame
//Node Js Program 
//Replace substring with another string in a string
class MyString
{
	// Replace substring with a another String
	// Here text is given string
	// And substring which is modifying of given pattern
	// pattern is other substring
	modified(text, substring, pattern)
	{
		var result = "";
		var status = false;
		//loop controlling variables
		var i = 0;
		var j = 0;
		process.stdout.write(" Given Text : " + text + "\n");
		process.stdout.write(" Given Substring : " + substring + "\n");
		process.stdout.write(" Given Pattern : " + pattern + "\n");
		//Loop iterating String(char) elements
		for (i = 0; i < text.length; ++i)
		{
			//reset status flag
			status = false;
			//check that first character of substring is exist or not in a given text
			if (text[i] == substring[0])
			{
				status = true;
				//Check that text contains substring or not?
				for (j = 1; status == true && j < substring.length && (i + j) < text.length; ++j)
				{
					if (text[i + j] != substring[j])
					{
						//When substring characters are not exist
						status = false;
					}
				}
				if (status == true && j < substring.length)
				{
					//When substring portion are not exist
					status = false;
				}
			}
			if (status == true)
			{
				//Add pattern
				result += pattern;
				i += substring.length - 1;
			}
			else
			{
				//Add single character
				result += text[i];
			}
		}
		return result;
	}
}

function main()
{
	var obj = new MyString();
	var text = "super6and4superstarsupersongsupergame";
	var substring = "super";
	var pattern = "good";
	text = obj.modified(text, substring, pattern);
	process.stdout.write(" Result : " + text + "\n");
}
main();

Output

 Given Text : super6and4superstarsupersongsupergame
 Given Substring : super
 Given Pattern : good
 Result : good6and4goodstargoodsonggoodgame
# Python 3 Program 
# Replace substring with another string in a string
class MyString :
	#  Replace substring with a another String
	#  Here text is given string
	#  And substring which is modifying of given pattern
	#  pattern is other substring
	def modified(self, text, substring, pattern) :
		result = ""
		status = False
		# loop controlling variables
		i = 0
		j = 0
		print(" Given Text : ", text ,"\n", end = "")
		print(" Given Substring : ", substring ,"\n", end = "")
		print(" Given Pattern : ", pattern ,"\n", end = "")
		# Loop iterating String(char) elements
		while (i < len(text)) :
			# reset status flag
			status = False
			# check that first character of substring is exist or not in a given text
			if (text[i] == substring[0]) :
				status = True
				# Check that text contains substring or not?
				j = 1
				while (status == True and j < len(substring) and(i + j) < len(text)) :
					if (text[i + j] != substring[j]) :
						# When substring characters are not exist
						status = False
					
					j += 1
				
				if (status == True and j < len(substring)) :
					# When substring portion are not exist
					status = False
				
			
			if (status == True) :
				# Add pattern
				result += pattern
				i += len(substring) - 1
			else :
				# Add single character
				result += text[i]
			
			i += 1
		
		return result
	

def main() :
	obj = MyString()
	text = "super6and4superstarsupersongsupergame"
	substring = "super"
	pattern = "good"
	text = obj.modified(text, substring, pattern)
	print(" Result : ", text ,"\n", end = "")

if __name__ == "__main__": main()

Output

 Given Text :  super6and4superstarsupersongsupergame
 Given Substring :  super
 Given Pattern :  good
 Result :  good6and4goodstargoodsonggoodgame
# Ruby Program 
# Replace substring with another string in a string
class MyString

	#  Replace substring with a another String
	#  Here text is given string
	#  And substring which is modifying of given pattern
	#  pattern is other substring
	def modified(text, substring, pattern)
	
		result = ""
		status = false
		# loop controlling variables
		i = 0
		j = 0
		# Display given information
		print(" Given Text : ", text ,"\n")
		print(" Given Substring : ", substring ,"\n")
		print(" Given Pattern : ", pattern ,"\n")
		# Loop iterating String(char) elements
		while (i < text.length())
		
			# reset status flag
			status = false
			# check that first character of substring is exist or not in a given text
			if (text[i] == substring[0])
			
				status = true
				# Check that text contains substring or not?
				j = 1
				while (status == true && j < substring.length() && (i + j) < text.length())
				
					if (text[i + j] != substring[j])
					
						# When substring characters are not exist
						status = false
					end
					j += 1
				end
				if (status == true && j < substring.length())
				
					# When substring portion are not exist
					status = false
				end
			end
			if (status == true)
			
				# Add pattern
				result += pattern
				i += substring.length() - 1
			else
			
				# Add single character
				result += text[i]
			end
			i += 1
		end
		return result
	end
end
def main()

	obj = MyString.new()
	text = "super6and4superstarsupersongsupergame"
	substring = "super"
	pattern = "good"
	text = obj.modified(text, substring, pattern)
	print(" Result : ", text ,"\n")
end
main()

Output

 Given Text : super6and4superstarsupersongsupergame
 Given Substring : super
 Given Pattern : good
 Result : good6and4goodstargoodsonggoodgame
//Scala Program 
//Replace substring with another string in a string
class MyString
{
	// Replace substring with a another String
	// Here text is given string
	// And substring which is modifying of given pattern
	// pattern is other substring
	def modified(text: String, substring: String, pattern: String): String = {
		var result: String = "";
		var status: Boolean = false;
		//loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		//Display given information
		print(" Given Text : " + text + "\n");
		print(" Given Substring : " + substring + "\n");
		print(" Given Pattern : " + pattern + "\n");
		//Loop iterating String(char) elements
		while (i < text.length())
		{
			//reset status flag
			status = false;
			//check that first character of substring is exist or not in a given text
			if (text(i) == substring(0))
			{
				status = true;
				//Check that text contains substring or not?
				j = 1;
				while (status == true && j < substring.length() && (i + j) < text.length())
				{
					if (text(i + j) != substring(j))
					{
						//When substring characters are not exist
						status = false;
					}
					j += 1;
				}
				if (status == true && j < substring.length())
				{
					//When substring portion are not exist
					status = false;
				}
			}
			if (status == true)
			{
				//Add pattern
				result += pattern;
				i += substring.length() - 1;
			}
			else
			{
				//Add single character
				result += text(i);
			}
			i += 1;
		}
		return result;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "super6and4superstarsupersongsupergame";
		var substring: String = "super";
		var pattern: String = "good";
		text = obj.modified(text, substring, pattern);
		print(" Result : " + text + "\n");
	}
}

Output

 Given Text : super6and4superstarsupersongsupergame
 Given Substring : super
 Given Pattern : good
 Result : good6and4goodstargoodsonggoodgame
//Swift Program 
//Replace substring with another string in a string
class MyString
{
	// Replace substring with a another String
	// Here str_data is given string
	// And sub_string which is modifying of given pattern
	// pattern_data is other substring
	func modified(_ str_data: String, _ sub_string: String, _ pattern_data: String) -> String
	{
		var result: String = "";
		var status: Bool = false;
      	let text = Array(str_data);
        let substring = Array(sub_string);
      	let pattern = Array(pattern_data);
		//loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		print(" Given Text : ", str_data );
		print(" Given Substring : ", sub_string );
		print(" Given Pattern : ", pattern_data );
		//Loop iterating String(char) elements
		while (i < text.count)
		{
			//reset status flag
			status = false;
			//check that first character of substring is exist or not in a given text
			if (text[i] == substring[0])
			{
				status = true;
				//Check that text contains substring or not?
				j = 1;
				while (status == true && j < substring.count && (i + j) < text.count)
				{
					if (text[i + j] != substring[j])
					{
						//When substring characters are not exist
						status = false;
					}
					j += 1;
				}
				if (status == true && j < substring.count)
				{
					//When substring portion are not exist
					status = false;
				}
			}
			if (status == true)
			{
				//Add pattern
				result += pattern;
				i += substring.count - 1;
			}
			else
			{
				//Add single character
				result += String(text[i]);
			}
			i += 1;
		}
		return result;
	}
}
func main()
{
	let obj: MyString = MyString();
	var text: String = "super6and4superstarsupersongsupergame";
	let substring: String = "super";
	let pattern: String = "good";
	text = obj.modified(text, substring, pattern);
	print(" Result : ", text ,"\n", terminator: "");
}
main();

Output

 Given Text :  super6and4superstarsupersongsupergame
 Given Substring :  super
 Given Pattern :  good
 Result :  good6and4goodstargoodsonggoodgame

Comment

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