0

This is a program to concatenate strings using malloc

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

char *sconcat(char *ptr1,char *ptr2);

void main()
{
char string1[20],string2[20],*ptr;
clrscr();

printf("enter string 1: ");
gets(string1);

printf("enter string 2: ");
gets(string2);

ptr=sconcat(string1,string2);

printf("output string : %s",ptr);
getch();
}

char *sconcat(char *ptr1,char *ptr2)
{
int len1,len2,i,j;
char *ptr3;

len1=strlen(ptr1);
len2=strlen(ptr2);

ptr3=(char *)malloc((len1+len2+1)*sizeof(char));

for(i=0;ptr1[i]!='\0';i++)
ptr3[i]=ptr1[i];

j=i;i=0;
for(;ptr2[j]!='\0';j++,i++)
ptr3[j]=ptr2[i];

ptr3[j]='\0';
return(ptr3);
}

output:
enter string 1 : this program does
enter string 2 : not give output
output string : this program does 

What correction is needed to concatenate strings. When I use char string1[20],string2[20],*ptr; after void main(),

output:
enter string 1 : is this 
enter string 2 : correct ?
output string : correct? ?
user3018725
  • 65
  • 1
  • 6

2 Answers2

4

The test in your second for loop is incorrect; it should be ptr2[i] != '\0', not ptr2[j] != '\0'.

Several remarks on the code:

  • Don't cast the return value of malloc.
  • sizeof(char) is 1 by definition, so multiplying by sizeof(char) only makes the code harder to read.
  • Declare the parameters of sconcat as const char *, since they're not modifying the strings they receive.
  • malloc can return NULL; you must handle that case in your program, for example by displaying an error message and exiting.
  • gets is unsafe and will crash your program if the user enters more characters than were allocated. Replace gets(string) with fgets(string, sizeof(string), stdin), and getting rid of the trailing newline.
  • clrscr() and getch(), as well as the infamous <conio.h> header, are not standard C and are non-portable; avoid them in simple programs like this one.
Community
  • 1
  • 1
user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • thanx...its working now....if not `clrscr()` and `getch()`, `` header,then how which statements must replace them? – user3018725 Nov 23 '13 at 15:41
  • if i declare string length as 10 instead of 20, i get `output:enter string 1: i love ; enter string 2 : my india ; output string : i love m` ....does not work – user3018725 Nov 23 '13 at 15:50
  • @user4815162342 Substituting `gets()` with `fgets()`, though a good idea has a side effect: `fgets()` retains the '\n'. `gets()` does not. An closer alternative is `gets_s()`. – chux - Reinstate Monica Nov 23 '13 at 16:06
  • instead of `gets` i have used`fgets(stdin, string1, sizeof(string1)); gives error1:`type mismatch in parameter '_n' in call to 'fgets'`; error 2:`type mismatch in parameter '_n' in call to 'fgets'` and using `gets_s` gives `undefined symbol _gets_s` – user3018725 Nov 23 '13 at 16:18
  • @chux Good point, I've now amended the answer to mention the trailing newline. `gets_s` only made it to C11, and is still not available on many systems - for example, my fairly up-to-date ArchLinux doesn't have a manpage for `gets_s`, nor is it present in the system include files. – user4815162342 Nov 23 '13 at 19:05
  • @user3018725 re `fgets` my answer switched the order of arguments - I've now updated the answer with the correct order. – user4815162342 Nov 23 '13 at 19:06
  • @user3018725 re `clrscr` and friends, my point is that they are non-portable (they won't work on Linux or Mac), and that you don't need them in such a simple program. If you refrain from manipulating the "console", your program will remain usable from pipelines, where the standard input and/or the standard output are redirected to a file or to another program. – user4815162342 Nov 23 '13 at 19:08
3

You can more simply use strcat

printf("enter string 1: ");
gets(string1);

printf("enter string 2: ");
gets(string2);

strcat(string1,string2);

It would, however, change string1 so you might want to use strcpy too (to copy string1 to another string and then return it).

amdixon
  • 3,814
  • 8
  • 25
  • 34
flyman
  • 210
  • 4
  • 15