used and append.used are the length of strigs. and data is pointer to array.
Hi
I am trying to concatenate two strings in the class using temperory pointer temp. but it is not printing anything.Could you tell me what is wrong with my for loops because program works wit actual strcat().
Str Str:: operator+(const str & st1) const
{
int used1=used+st1.used;
char* ch1=newchar [used1+1];
int i;
for (i=0;i<used;i++)
ch1[i]=data[i];
for (i;i<used1;i++)
ch1[i]=st1.data[i-used];
ch1[used1]='\0';
Str obj(ch1);
delete [ ] ch1;
return obj;
}
Hi.
This is my variant of your task. As I see you have mistake in the second for loop comparing i & used
Try to substitute the real values and you'll understand. Try to use less arguments.
P.s: sorry, my english is not good.
See I am giving used an initial value but I should not do that because may be my Str object has mich larger or smaller lenght tahn used. I really dont know what to do , should I pass another argument for number of used characters , how can I say to my constructors size of string by the fact that I should not care about null characyer.
Any help will be highly appriciated?
Thanks.
Str Str:: operator+(const Str & append) const
{
//let's think that your initial Str values are "dog" and "cat"
unsigned i=0,j=0;// as you see i=0
char *temp=newchar[used+append.used+1];
for(i=0,j=0; i<used;j++, i++ )
temp[i]=data[j];//here you copy "dog"
//and here your i=3, you're trying to compare 3 and 3. As a result you get nothing
//so change it to for(i,j=0 ; j<append.used; j++,i++)
for(i,j=0 ; i<append.used; j++,i++)
temp[i]=append.data[j];
temp[i]='\0';
return Str(temp);//this is bad way to do=) you have to remove memory
//that had been created by new operator using
//operator delete [] temp.
//But firstly you should create an Str object like Str obj(temp); delete [] temp;
}
my Str object has mich larger or smaller lenght tahn used. I really dont know what to do , should I pass another argument for number of used characters , how can I say to my constructors size of string by the fact that I should not care about null characyer.
It depend's on how you have declared your data argument.
Like this: char data[N]; or char * data;
so the first variant means that your data length is always N. The second means you have to use operator new and delete and data length can be changed.
You can count number of characters in input string using strlen() function from <cstring>
and try to be always sure that data[N-1]='\0'
You can use something like this if u don't want to use library function
1 2 3 4 5 6 7
int count(constchar* ch1)
{
int i=0;
while (*ch1++)
i++;
return i;
}
Hi
I am using char * data , and I am using operator new , and I used strlen() function . and I created Str obj(temp) and I delete[] temp then I return Str(obj), but now I am getting memory problem "HEAP CORRUPTION" . any help ?
here is my code:
The problem with String540::String540(constchar * s) is you need to determine the string length, then allocate enough memory to copy the string into. Your code just allocates 24 bytes, which has nothing to do with the actual string being copied in. It may be longer or shorter.
Thanks guys The purpose of my program is to write c++ string class which should be different from c string (c string is null terminated) but If I dont put the null character at the end of my for loop it appends some garbage characters at the end , so what should I do in order to avoid those characters. because for example if I have s1=Hello and s2=\0world then s1+s2 is just Hello which should not be like this for example in