concatination

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    Str Str:: operator+(const str & append) const
    {
    unsigned i,j;
     
    for(i=0,j=0 ; i<append.used; i++ ,j++)
     
    temp[i]=append.data[j];
     
    for(j=0 ; i<used; i++, j++)
     
    temp[i]=data[j];
     
    temp[i]='\0';
    }


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().
Where is temp declared?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Str Str:: operator+(const str & st1) const
{
    int used1=used+st1.used;
    char*  ch1=new char [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.
Last edited on
1
2
3
char*  ch1=new char [used1+1];
//...
ch1[used1+1]='\0'; // are you sure? 
Hi
Here is what I write, but it still does not work.
my other constructors are working but I dont know why this one is not doing any thing . any help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Str Str:: operator+(const Str & append) const
{
	unsigned i=0,j=0;

	char *temp=new char[used+append.used+1];
	
	for(i=0,j=0; i<used;j++, i++ )
	
		
		temp[i]=data[j];
	
	for(i,j=0 ; i<append.used; j++,i++)
	
		temp[i]=append.data[j];
	
	temp[i]='\0';
	
	
	return Str(temp);                
}
Last edited on

You are absolutely right , my append overloading is working but my Str (*char) no it is not working.
Here How I wrote that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

String540::String540(const char * s):
allocated(24) , used(20), data(new char[allocated])
{
	unsigned i=0;
	
	for(i=0; i<used; i++)
	{
		
		  data[i]=s[i];
	}
	
}









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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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=new char[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(const char* ch1)
{
    int i=0;
    while (*ch1++)
        i++;
    return i;
}






Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Str  Str:: operator+(const Str & append) const
{
	unsigned i=0,j=0;
	
	char *temp=new char[used+append.used+1];
	
	for(i=0,j=0; i<used;j++, i++ )
	
		
		temp[i]=data[j];
	
	for( j=0 ; i<append.used+used-1; j++,i++)
	
		temp[i]=append.data[j];
	
	temp[i]='\0';	
                Str obj(temp);
	delete[] temp;
		return Str(obj);              
}
Last edited on
The problem with String540::String540(const char * 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Str Str:: operator+(const Str & append) const
{
	unsigned i=0,j=0;
    	
	char *temp=new char[used+append.used];	
	
	for(i=0,j=0; i<used;j++, i++ )
	
		
		temp[i]=data[j];
	
	for( i=used,j=0 ; i<append.used+used; j++,i++)
	
		temp[i]=append.data[i-used];
	
	temp[i]='\0';
	Str obj(temp);
	delete [] temp;

		return Str(obj);            
}

I am getting memory heap corruption error message , any help ?
Show all your constructors and operator=.
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Str Str:: operator+(const Str & append) const
{
	unsigned i=0,j=0;
    	
	char *temp=new char[strlen(data)+strlen(append.data)];	
	
	for(i=0,j=0; i<used;j++, i++ )
	
		
		temp[i]=data[j];
	
	for( i=used,j=0 ; i<append.used+used; j++,i++)
	
		temp[i]=append.data[i-used];
	
	temp[i]='\0';
	
	return Str(temp);                
}

You can see that I have temp[i]='\0' , it means it will append null character at the end , what should I do in order to solve this problem.
Topic archived. No new replies allowed.