Problem with string function

Hi

I am trying to create a function that append one function to the other like if i have this:

String1 ="12345"
String2 ="67890"

the function will return this string "1234567890".

but i when i execute the program i get the first string only. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Test5_console.cpp : 
//


#include <iostream>

#include <conio.h>



using namespace std;

//The length of string function
int StringLength (char *p)
{
    //it's simple just increase i until it found the null-terminating character
	int i=0;
	while(p[i] != '\0')
	{
	 i++;
	}

  return i+1;
}


//Appending one string to another function
char *cat(char *p1,char *p2)
{
  int length = 0,length1=0,length2=0;

  //StringLeng thing the size of the new string that will be created
  //which is the length of p1 + length of p2

  length1 = StringLength(p1);
  length2 = StringLength(p2);
  length = length1 + length2;

  
  char *p3=new char [length+1];
  
  //Puting string one in the beginning of he new string 
  for (int i = 0 ; i<length1 ; i++)
  {
   p3[i]=p1[i];
  }
   

  //Puting string two where the first string is finished

  int j=0;
  
  for (int i = length1 ; i< length ; i++)
  {
   p3[i]=p2[j];
   j++;
  }
   


  return p3; 
}

int main ()
{

  char *a="The first string";
  char *b="The second string";
  char *c;

  c=cat(a,b);

  cout<<c;
  
  delete [] a;
  delete [] b;
  delete [] c;

  a=NULL;
  b=NULL;
  c=NULL;

  //Press any key code to continue code...
  cout<<"\n\n\n\tPress any key code to continue..."<<endl;;
  _getch();
	return 0;
}


And here is the output:


The first string


        Press any key code to continue...
Last edited on
I've made youre code a litle shorter etc...
however now work's :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

int StringLength (char* p) {
	short i=0;
	while(p[i]) ++i;
        return i;
}

char* cat(char* p1, char* p2) {

	const short length1 = StringLength (p1);
	const short length2 = StringLength (p2);
	const short length = length1 + length2;
	char *p3 = new char;
	static short i = 0, j = length2, z = 0;

	for (; i < length1 ; i++)
		p3 [i] = p1 [i];

	for (; i < length ; i++, z++)
		p3 [i] = p2 [z];

	return p3; 
}

int main () {

	char* a = "The first string";
	char* b = "The second string";
	char* c = cat (a, b);

	cout << endl << endl << c << endl;
  
	system ("pause");
	return 0;
}

sasanet croatia
Last edited on
Heavens...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

string cat(const string& str1,const string& str2)
{
  return str1+str2;
}

int main()
{
  string a="The first string";
  string b="The second string";
  string c=cat(a,b);
  cout << c << endl;
}


As for your code:
In line 23, that's one too much. Should be return i;
Line 67/68: You cannot assign a string literal to a non-const pointer. That's not valid C++.
Line 75/76: ...let alone can you delete[] them. No new[] - no delete[].
Line 30/79/80/81: Pointless. Your compiler should warn you about that.
or use string functions and not your own:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main () {
	char* a = "The first string";
	char* b = "The second string";
	short x = strlen (a) + strlen (b) + 1;
	char* c = new char [x];
	strcpy (c, a);
	strcat (c, b);
	cout << c << endl;
	delete c;
	system ("pause");
	return 0;
}


sasanet
Last edited on
sasanet: This
a) Uses cstrings, which are usually to be avoided in C++ unless you really need them
b) Doesn't even compile because cstring is not included.
c) Deletes an array as a single object.
Last edited on
@sasanet

line 13 should be delete [] c

Don't want any memory leaks ;)


EDIT: Just realizes that hanst99 covers this in section of "c" in his post.
Last edited on
hanst99,

you are apsolutly right about cstring ... i've forgot include it.

but however IT COMPILE on my visual C++ express 2010 :)

chears!
It may compile because <string> may include <cstring>.
Last edited on
sasanet:

thanks man.
Great code it worked perfectly in "Dev-c++", but when i tried "Visual Studio 2010" it gave me this output:

The first stringThe second string☺4


Can you tell me why there is "☺4" in the output?

Last edited on

Athar:

Thanks for the reply. but i know about the string library. but i want to train on strings using pointers and and function i am very beginner at using pointers.


thanks for the advises they are great. except this one:


Line 67/68: You cannot assign a string literal to a non-const pointer. That's not valid C++.


i seen everyone doing it without using const pointer, even "sasanet" before you.
Are you sure about what you wrote or am i the one who didn't understand you right?
Last edited on
i seen everyone doing it without using const pointer, even "sasanet" before you.
Are you sure about what you wrote or am i the one who didn't understand you right?

Yes, I am sure. A string literal is a constant character array which can be implicitly converted to a constant char pointer to its first character. The C++ standard added an exception to allow the cast of a string literal to a non-const char pointer to maintain backwards compatibility with old code. However, this has been deprecated since the day it was added and should not be used. As it is, trying to modify a string literal via such a non-const pointer leads to undefined behavior, even though the conversion being allowed by the compiler might suggest otherwise.
Last edited on
Athar:

thanks man for the information. Than the code at that part should have been like this:

1
2
   const char *a="The first string";
   const char *b="The second string";


right?
Topic archived. No new replies allowed.