Saturday, January 26, 2013

Write a C++ program that gets two strings from input and stores them in variables such as str1 and str2

Exercise 7) String 
Write a C++ program that gets two strings from input and stores them in variables such as str1 and str2. Then you should delete all possible str1 in str2. Note that after deletion of str1 in str2, you need to check again the inclusion of str1 in str2. That means the final printed result at the screen shouldn’t include any str1. 

Str1? ab
Str2? sksaabbkjsdaabbksjkabksjsababaaab
Result? skskjsdksjkksjsaa

Solution-


#include<conio.h>
#include<iostream>
#include<String.h>
#include<stdio.h>

using namespace std;
//-------defining global variables and initializing them------------- 
string str1,str2;
char str3[256];
bool a= true;

void String1();

int main()
{
    char redo;
    
    //--------Printing my name on screen----------------    
    cout<<        "Welcome to the  program 2.3 written by Your Name"<<endl;
    cout<<"**************************************************************************"<<endl;
    cout<<endl<<endl<<endl;
    //--here do loop is used so that the program can be used more then one time
//without exiting the run screen---------------------------   
    
    cout<<"\nEnter the First string: ";
    cin>>str1;
    cout<<"\nEnter the Second string:";
    cin>>str2;
    
    while(a)
    {
    String1();
    }
    
    cout<<"New  String after deleting first string from second : "<<endl<<str2;
     cout<<endl<<endl;
     cout<<"enter y or Y to continue:";
           cin>>redo;
           cout<<endl<<endl;
          
    
    getch();
}
void String1()
{
     //-------defining local variables and initializing them------------- 
     int len1,len2;
     int len3,k=0,b=0, match=0,i,j,count=0;
     len1=str1.length();
     len2=str2.length();
     
     for(i=0;i<len2;i++)
     {
         b=i;   
          for(j=0;j<len1;j++)
          {
             if(str2[b]==str1[j])
             {
              match=  match+1;
              b=b+1;
             }
             else
              break;      
          }
   
          if(match!=len1)
          {
            match=0;             
  
            str3[count]=str2[i];
            count++;
   
          }
          else
          {
             match=0; 
             a=true;
             i=i+len1-1;
          }            
     }
  
     str2=str3;
     if(str2.length()==len2)
     {
         a=false;                   
     }
     str3[0]='\0';
     
     for(int i=0;i<len2;i++)
             str3[i]='\0';
  
}




---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
IT Certification Category (English)640x480

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com


Top Online Courses From ProgrammingKnowledge

Python Course http://bit.ly/2vsuMaS
Java Coursehttp://bit.ly/2GEfQMf
Bash Coursehttp://bit.ly/2DBVF0C
Linux Coursehttp://bit.ly/2IXuil0
C Course http://bit.ly/2GQCiD1
C++ Coursehttp://bit.ly/2V4oEVJ
PHP Coursehttp://bit.ly/2XP71WH
Android Coursehttp://bit.ly/2UHih5H
C# Coursehttp://bit.ly/2Vr7HEl
JavaFx Coursehttp://bit.ly/2XMvZWA
NodeJs Coursehttp://bit.ly/2GPg7gA
Jenkins Course http://bit.ly/2Wd4l4W
Scala Coursehttp://bit.ly/2PysyA4
Bootstrap Coursehttp://bit.ly/2DFQ2yC
MongoDB Coursehttp://bit.ly/2LaCJfP
QT C++ GUI Coursehttp://bit.ly/2vwqHSZ