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? ?
1
  • The return type of strlen() is size_t. You will find greater portability with size_t len1,len2,i,j Commented Nov 23, 2013 at 15:47

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

7 Comments

thanx...its working now....if not clrscr() and getch(), <conio.h> header,then how which statements must replace them?
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
@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().
instead of gets i have usedfgets(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
@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.
|
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).

2 Comments

i know about strcat but i want to accomplish it this way.
Given OP's char string1[20],string2[20], "enter string 1 : this program does" and "enter string 2 : not give output", the suggested strcat(string1,string2) writes outside string1[].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.