Question 12: Write a C/C++ program to replace a substring. The function should accept  three C­string or string object arguments. Let’s call them string1, string2, and  string3. It should search string1 for all occurrences of string2. When it finds an  occurrence of Programming Challenges 595 string2, it should replace it with  string3. 

February 13, 2019
Question 12: Write a C/C++ program to replace a substring. The function should accept  three C­string or string object arguments. Let’s call them string1, string2, and  string3. It should search string1 for all occurrences of string2. When it finds an  occurrence of Programming Challenges 595 string2, it should replace it with  string3. 
/*
Name:                   Mudassar Ali
Roll:                   BCSF15M045   [Morning]
Assignment:             No# 05
Teacher:                Miss Sadia Shahzad
*/
#include <iostream>
#include <cstring>

using namespace std;
int main()
{
    cout<<"\tThis Programme will replace the second string with third string!!\n\n";

    char str1[50];  // To hold the first string contents.
    char str2[10];  // To hold the first string contents.
    char str3[10];  // To hold the first string contents.
    char strn[250]={0}; // To store the contents of string one.
    int i=0,k=0,l=0,m=0;

    cout<<"Enter the 1st string:";
    cin.getline(str1,50); // Inputting the first string.
    cout<<"Enter the 2nd string select to replace with:";
    cin>>str2;            // Inputting the second string.
    cout<<"Enter the 3rd string select to replace with with 2nd string:";
    cin>>str3;            // Inputting the third string.

    int len = strlen(str2); // checking the length of string two
     len = len - 1;

    for( i =0; str1[i]!=NULL;i++)
    {
        strn[l]=str1[i];           // Writing the contents of string one to new string.

        if(str1[i]==str2[k])       // Comparing the contents of string one with string two.
        {

                k++;
              if(str2[k]==NULL)        // After checking the condition of the string two.
              {
                  for( l =l-len;str3[m]!=NULL;l++)// writing the contents of string three to new string.
                  {
                      strn[l]=str3[m];
                      m++;
                  }
                  l--;
              }
        }
        else                     // To ensure that if the consecutive contents of strings are same.
        {
            if(str1[i]==str2[k])
                k++;
            else
                k=0;
        }
        l++;
    }

    strn[l]=NULL;       // Storing the NULL at the last index after sentence.
    cout<<"\nNew string is: "<<strn;
return 0;
}

No comments:

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
Powered by Blogger.