A
ashjas
Hello,
I have run this logic on turboc++ 3.0 and it is working fine on it but
its not running on msvs2008 c++.
i am not able to assign the value like this *temp=*main,where main and
temp are char pointers.
there is some runtime access violation error on msvs..that i am not
able to work out how to resolve..
kind attention and feedback would be invaluable for me...
thanks...the code is as below..
// C++Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
using namespace std;
int i;
int searchsub(char mainstr[],char fndstr[])
{
int mainlen=0,sublen=0,flag=-1,index=0;
for( i=0;mainstr!='\0';i++)
mainlen++;
for( i=0;fndstr!='\0';i++)
sublen++;
for(i=0;i<mainlen;i++)
{
index=i;
if(mainstr==fndstr[0])
{
for(int k=0;k<sublen;k++)
{
if(mainstr[i++]!=fndstr[k])
{
flag=0;
break;
}
else flag=1;
}
if(flag==1)
break;
i=index;
}
}
if(flag==1)
return index;
else return -1;
}
char * strrep(char * main,char* sub,char* rep)
{
int sublen=0,mainlen=0,replen=0;
char* save=main;
// cout<<rep;
// cout<<main<<" "<<save;
char*temp=new char;
//*temp='k';
// cout<<"-"<<temp<<"1";
for( i=0;*rep!='\0';i++,rep++)
replen++;
rep=rep-i;
//for( i=0;*main!='\0';i++,main++)
// mainlen++;
//main=main-i;
int index=searchsub(main,sub);
for( i=0;*sub!='\0';i++,sub++)
sublen++;
//cout<<sublen;
sub=sub-i;
//cout<<sub;
int reststrindex=index+sublen;
// cout<<reststrindex;
main=main+reststrindex;
//cout<<main;
for(i=0;*main!='\0';i++,temp++,main++)
*temp=*main;
*temp='\0';
main=main-i-reststrindex;
//cout<<main;
int templen=0;
temp=temp-i;
//cout<<temp;
for( i=0;*temp!='\0';i++,temp++)
templen++;
temp=temp-i;
//cout<<temp;
//cout<<main;
main=main+index;
//cout<<main;
for(i=index;i<index+replen;i++)
{
*main=*rep;
main++;
rep++;
}
for(i=0;i<templen;i++)
{
*main=*temp;
main++;
temp++;
}
*main='\0';
main=save;
return main;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<strrep("abcdefghij","cde","peternorton");
return 0;
}
I have run this logic on turboc++ 3.0 and it is working fine on it but
its not running on msvs2008 c++.
i am not able to assign the value like this *temp=*main,where main and
temp are char pointers.
there is some runtime access violation error on msvs..that i am not
able to work out how to resolve..
kind attention and feedback would be invaluable for me...
thanks...the code is as below..
// C++Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
using namespace std;
int i;
int searchsub(char mainstr[],char fndstr[])
{
int mainlen=0,sublen=0,flag=-1,index=0;
for( i=0;mainstr!='\0';i++)
mainlen++;
for( i=0;fndstr!='\0';i++)
sublen++;
for(i=0;i<mainlen;i++)
{
index=i;
if(mainstr==fndstr[0])
{
for(int k=0;k<sublen;k++)
{
if(mainstr[i++]!=fndstr[k])
{
flag=0;
break;
}
else flag=1;
}
if(flag==1)
break;
i=index;
}
}
if(flag==1)
return index;
else return -1;
}
char * strrep(char * main,char* sub,char* rep)
{
int sublen=0,mainlen=0,replen=0;
char* save=main;
// cout<<rep;
// cout<<main<<" "<<save;
char*temp=new char;
//*temp='k';
// cout<<"-"<<temp<<"1";
for( i=0;*rep!='\0';i++,rep++)
replen++;
rep=rep-i;
//for( i=0;*main!='\0';i++,main++)
// mainlen++;
//main=main-i;
int index=searchsub(main,sub);
for( i=0;*sub!='\0';i++,sub++)
sublen++;
//cout<<sublen;
sub=sub-i;
//cout<<sub;
int reststrindex=index+sublen;
// cout<<reststrindex;
main=main+reststrindex;
//cout<<main;
for(i=0;*main!='\0';i++,temp++,main++)
*temp=*main;
*temp='\0';
main=main-i-reststrindex;
//cout<<main;
int templen=0;
temp=temp-i;
//cout<<temp;
for( i=0;*temp!='\0';i++,temp++)
templen++;
temp=temp-i;
//cout<<temp;
//cout<<main;
main=main+index;
//cout<<main;
for(i=index;i<index+replen;i++)
{
*main=*rep;
main++;
rep++;
}
for(i=0;i<templen;i++)
{
*main=*temp;
main++;
temp++;
}
*main='\0';
main=save;
return main;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<strrep("abcdefghij","cde","peternorton");
return 0;
}