need help to find and delete a string

Discussion in 'C++' started by arunlalds, Mar 17, 2010.

  1. arunlalds

    arunlalds Banned

    Joined:
    Mar 12, 2010
    Messages:
    43
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    student
    Location:
    India
    Write a c++ program to read a string. then accept another string a find the string on string1.if found the delete
    ex: independence

    pen

    final output

    indedence
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
  3. itstimetojazz

    itstimetojazz New Member

    Joined:
    Sep 9, 2009
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    int main()
    {         
                   char str1[MAX_PATH];
    	char str2[MAX_PATH];
    
    	cout<<"Enter a string"<<endl;
    	cin>>str1;
    	int n1;
    	n1=strlen(str1);
    	str1[n1]='\0';	//to ensure null characters are eliminated
    
    	cout<<endl;
    	cout<<"enter a substring to eliminate"<<endl;
    	cin>>str2;
    
    	int n2;
    	n2=strlen(str2);
    	str2[n2]='\0'; //to ensure null characters are eliminated
    
    	char *pdest;
    
    	pdest=strstr(str1,str2);
    
    
    	int result1;
    	result1=pdest-str1;
    
    	int len=strlen(str2);
    	
    	//loop to print the initials
    	for(int i=0; i<result1;i++)
    	{
    		cout<<str1[i];
    	}
    
    	//print the remaining characters
    	cout<<pdest+len<<endl;
    
    return 0;
    }
     
    Last edited by a moderator: Mar 18, 2010

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice