Program to Replace a Substring in a String | FACE Prep

Program to replace a substring in a string is discussed here. The steps for replacing a substring with another string are given below.

Input:

hi hello
string to be replaced: hi
string to be replaced with: hey

output: hey hello

Algorithm to Replace a substring in a string

  • Input the full string (s1).
  • Input the substring from the full string (s2).
  • Input the string to be replaced with the substring (s3).
  • Find the substring from the full string and replace the new substring with the old substring (Find s2 from s1 and replace s1 by s3).

Program to Replace a substring in a string

To receive Placement preparation tips and Placement related updates, come join the largest placement Community on FACE’s Telegram and WhatsApp, and take your placement preparation to the next level.
 
Join here:
Telegram: https://t.me/placements2020
WhatsApp: http://bit.ly/2MgoYMR
// C code to replace a substring in a string

#include<stdio.h>
#include<string.h>

void replaceSubstring(char [],char[],char[]);

main()
{
char string[100],sub[100],new_str[100];
printf("\nEnter a string: ");
gets(string);
printf("\nEnter the substring: ");
gets(sub);
printf("\nEnter the new substring: ");
gets(new_str);
replaceSubstring(string,sub,new_str);
printf("\nThe string after replacing : %s\n",string);
}

void replaceSubstring(char string[],char sub[],char new_str[])
{
int stringLen,subLen,newLen;
int i=0,j,k;
int flag=0,start,end;
stringLen=strlen(string);
subLen=strlen(sub);
newLen=strlen(new_str);

for(i=0;i<stringLen;i++)
{
flag=0;
start=i;
for(j=0;string[i]==sub[j];j++,i++)
if(j==subLen-1)
flag=1;
end=i;
if(flag==0)
i-=j;
else
{
for(j=start;j<end;j++)
{
for(k=start;k<stringLen;k++)
string[k]=string[k+1];
stringLen--;
i--;
}

for(j=start;j<start+newLen;j++)
{
for(k=stringLen;k>=j;k--)
string[k+1]=string[k];
string[j]=new_str[j-start];
stringLen++;
i++;
}
}
}
}
// C++ code to replace a substring in a string

#include <iostream.h>
#include <cstring>
using namespace std;

int main()
{
string str, str2, str3;
cout<<"Enter the main String: ";
cin>>str;
cout<<"Enter the string to be replaced: ";
cin>>str2;
int str2len = str2.length();
cout<<"Enter the replacing string: ";
cin>>str3;
str.replace(str.find(str2),str2.size(),str3);
cout<<str;
return 0;
}
// Java code to replace a substring in a string

import java.util.Scanner;
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = sc.nextLine();
System.out.println("Enter the string to be replaced: ");
String old_str = sc.nextLine();
System.out.println("Enter the new string: ");
String new_str = sc.nextLine();
String replaced = str.replace(old_str, new_str);
System.out.println("replaced string: " + replaced);
}
}
# Python code to replace a substring in a string

string = input("Enter the full string: ")
substring = input("Enter the substring to be replaced:")
replacing_string = input("Enter the string to be replaced:")
print (string.replace(substring, replacing_string))

Output:

replace a substring with a string

 

replace a substring with a string

 

replace a substring in a string