Write a Program to Replace substring in a string

07 January 2023

Write a Program to Replace substring in a string



If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get a string as input from the user and then get another string which has to be removed from the string. Get the third input, the new substring which is placed in that substring position. Finally print the output by replacing the substring with new string.

 

Input

Enter a string

talentbattle

Enter the substring to be removed:

talent

Enter the new substring:

student

 

 

Output

The new string:

studentbattle

 

C Program

#include <stdio.h>

#include <string.h>

void replace(char str[],char sub[],char nstr[])

{

    int strLen,subLen,nstrLen;

    int i=0,j,k;

    int flag=0,start,end;

    strLen=strlen(str);

    subLen=strlen(sub);

    nstrLen=strlen(nstr);

    for(i=0;i<strLen;i++)

    {

        flag=0;

        start=i;

        for(j=0;str[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<strLen;k++)

                    str[k]=str[k+1];

                strLen--;

                i--;

            }

 

            for(j=start;j<start+nstrLen;j++)

            {

                for(k=strLen;k>=j;k--)

                    str[k+1]=str[k];

                str[j]=nstr[j-start];

                strLen++;

                i++;

            }

        }

    }

}

 

int main()

{

    char str[20],sub[20],nstr[50];

    printf("Enter a string: ");

    scanf("%s",str);

    printf("Enter the substring to be removed: ");

    scanf("%s",sub);

    printf("Enter the new substring: ");

    scanf("%s",nstr);

    replace(str,sub,nstr);

    printf("The new string: %s",str);

    return 0;

}

 

C++ Program

#include <iostream>

#include <string.h>

using namespace std;

void replace(char str[],char sub[],char nstr[])

{

    int strLen,subLen,nstrLen;

    int i=0,j,k;

    int flag=0,start,end;

    strLen=strlen(str);

    subLen=strlen(sub);

    nstrLen=strlen(nstr);

    for(i=0;i<strLen;i++)

    {

        flag=0;

        start=i;

        for(j=0;str[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<strLen;k++)

                    str[k]=str[k+1];

                strLen--;

                i--;

            }

 

            for(j=start;j<start+nstrLen;j++)

            {

                for(k=strLen;k>=j;k--)

                    str[k+1]=str[k];

                str[j]=nstr[j-start];

                strLen++;

                i++;

            }

        }

    }

}

 

int main()

{

    char str[20],sub[20],nstr[50];

    cout<<"Enter a string: ";

    cin>>str;

    cout<<"Enter the substring to be removed: ";

    cin>>sub;

    cout<<"Enter the new substring: ";

    cin>>nstr;

    replace(str,sub,nstr);

    cout<<"The new string: "<<str<<endl;

    return 0;

}

 

Java

import java.util.Scanner;

public class Main

{

              public static void main(String[] args) {

                             Scanner sc = new Scanner(System.in);

     System.out.print("Enter a string: ");

     String str = sc.nextLine();

     System.out.print("Enter the string to be removed: ");

     String sub = sc.nextLine();

     System.out.print("Enter the new string: ");

     String newstr =sc.nextLine();

 

     String repstr = str.replace(sub, newstr);

     System.out.println("New String is :"+repstr);

              }

}

 

Python

Str=input("Enter a string: ")

sub=input("Enter string to be removed: ")

nstr=input("Enter ne string: ")

Str=Str.replace(sub,nstr)

print("New string: ")

print(Str)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Related Articles

Ask Us Anything !