Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   need help to find and delete a string (http://www.go4expert.com/forums/help-delete-string-t21389/)

arunlalds 17Mar2010 14:06

need help to find and delete a string
 
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

virxen 17Mar2010 16:09

Re: need help to find and delete a string
 
check this

http://www.cplusplus.com/reference/c...string/strstr/

itstimetojazz 18Mar2010 13:36

Re: need help to find and delete a string
 
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;
}



All times are GMT +5.5. The time now is 21:02.