// Test5_console.cpp :
//
#include <iostream>
#include <conio.h>
usingnamespace 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=newchar [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...
#include <iostream>
#include <string>
usingnamespace 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.
#include <iostream>
#include <string>
usingnamespace std;
int main () {
char* a = "The first string";
char* b = "The second string";
short x = strlen (a) + strlen (b) + 1;
char* c = newchar [x];
strcpy (c, a);
strcat (c, b);
cout << c << endl;
delete c;
system ("pause");
return 0;
}
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.
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?
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.