Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

CONCATENATION OF TWO STRINGS USING POINTER, it is not working. pls help me..........

C#
#include<iostream>
using namespace std;

int main()
{
    char *str1 = "krishna";
    char *str2 = "Prasad";
    char *str3;
    int count = 0;
    int i,j;
    for(i=0;*str1!=NULL;i++,*str1++)
    {
        count++;
    }
    for(j= 0; *str2!=NULL;j++,*str2++)
    {
        count++;
    }
    //str3 =  new char[sizeof(str1)+sizeof(str2)];
    str3 = (char*) malloc(13);


    for(i=0;str1[i]!=NULL;i++)
    {
        str3[i] = str1[i];
    }

    for(j= 0; str2[j]!=NULL;j++,i++)
    {
        str3[i] = str2[j];

    }
    cout<<endl;
    cout<<"String 3 is = ";

    while(*str3)
    {
        cout<<*str3++;
    }
    return 0;
}
Posted
Comments
Dave Kreskowiak 24-Jul-15 15:03pm    
First, WHY ARE YOU YELLING AT PEOPLE?

Second, before anyone is going to touch this, you might want to define exactly what you mean by "it is not working". That's not a problem description.

What DOES happen when you run this?
[no name] 24-Jul-15 15:09pm    
In addition to what Dave said, running your code under your debugger should tell you what you code is doing and why. Learn how to debug your code.
CPallini 24-Jul-15 15:52pm    
Why are you using C-like strings in C++?

When using malloc or new to allocate an array of characters for a nul terminated string, you need to include one extra character for the terminating nul.

Replace this:
C
str3 = (char*) malloc(13);


with this:
C
str3 = (char*) malloc(count + 1);


or this:
C
str3 = new char[count + 1];


Don't forget to terminate the new string with a nul character.

Replace this:
C
for(j= 0; str2[j]!=NULL;j++,i++)
{
    str3[i] = str2[j];
}


with this:

C
for(j=0; str2[j]!=0; j++,i++)
{
    str3[i] = str2[j];
}
str3[i] = 0;


Now that the new string is nul terminated, you can replace this:

C
while(*str3)
{
    cout<<*str3++;
}


with this:

C
cout << str3;


The following code tells me you tried something but it did not work.

C
//str3 =  new char[sizeof(str1)+sizeof(str2)];


The reason is the sizeof operator does not give you the length of a string but the size of the string pointer.
 
Share this answer
 
As suggested you should properly handle string terminators. Moreover, you should handle a possibly failing call to malloc and properly release dynamically allocated memory.

C
 #include <stdio.h>
 #include <stdlib.h>

 int my_strlen(const char * s)
 {
   int l = 0;
   while ( s[l] ) ++l;
   return l;
 }

 int main()
 {
   const char *s1 = "krishna";
   const char *s2 = "Prasad";

   int l1 = my_strlen(s1);
   int l2 = my_strlen(s2);

   char * s3 = (char *) malloc(l1 + l2 + 1); // make room for the string terminator

   if ( ! s3 ) return -1; // malloc failure

   int n1, n2, n3;
   n3 = 0;
   for (n1=0; n1 < l1; ++n1, ++n3)
     s3[n3] = s1[n1];

   for (n2=0; n2 < l2; ++n2, ++n3)
     s3[n3] = s2[n2];

   s3[n3] = '\0'; // append the string terminator

   printf("string 3 is %s\n", s3);

   free(s3); // release allocated memory

   return 0;
}
 
Share this answer
 
v2
Comments
Wendelius 27-Jul-15 3:42am    
My 5
CPallini 27-Jul-15 5:43am    
Thank you.
Sorry, but when I see code like that I can't resist :).
C language is basic enough to don't be worth to reinvent the wheel.
Use standard routines and code it as it should be:
C++
int main()
{
    char *str1 = "krishna";
    char *str2 = "Prasad";
    char *str3;

    str3 = malloc(strlen(str1) + strlen(str2) + 1);  //Allocate space for the sum of the lenghts of the 2 strings plus the final null.
    strcpy(str3, str1);    //Copy first string in the result
    strcat(str3, str2):    //Append the second string.

    cout << str3;     //print whole new string!

    return 0;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900