Developer community
+2 votes
6.4k views
program to concatenate two strings without using strcat() function.
asked by Advisor (7.1k points) 10 23 42 | 6.4k views

2 Answers

+2 votes
Best answer
strcat(string1,string2) is a C standard function declared in the header file string.h
The strcat() function concatenates string2, string1 and returns string1.

Program:
#include<stdio.h>
#include<string.h>
char *strct(char *c1, char *c2);
char *strct(char *c1, char *c2)
{
 ///////////////////strlen function returns length of argument string
 int i = strlen(c1);
 int k = 0;
  ///////////////////loops until null is encountered and appends string c2 to c1
 while (c2[k] != '\0') {
  c1[i + k] = c2[k];
  k++;
 }
 return c1;
}

int main()
{
 char string1[15] = "first";
 char string2[15] = "second";
 char *finalstr;

 printf("Before concatenation:"
 " \n string1 = %s \n string2 = %s", string1, string2);
   ///////////////////addresses of string1, string2 are passed to strct()
 finalstr = strct(string1, string2);
 printf("\nAfter concatenation:");
   ///////////////////prints the contents of string whose address is in finalstr
 printf("\n finalstr = %s", finalstr);
   ///////////////////prints the contents of string1
 printf("\n string1 = %s", string1);
   ///////////////////prints the contents of string2
 printf("\n string2 = %s", string2);
 return 0;
}

Output:
Before concatenation:
string1 = first
string2 = second
After concatenation:
finalstr = firstsecond
string1 = firstsecond
string2 = second
answered by Geek (15.7k points) 5 9 21
0 votes
strcat(string1,string2) is a C standard function declared in the header file string.h

The strcat() function concatenates string2, string1 and returns string1.

Program: Program to concatenate two strings

#include<stdio.h>

#include<string.h>

char *strct(char *c1, char *c2);

char *strct(char *c1, char *c2) {

//strlen function returns length of argument string

int i = strlen(c1);

int k = 0;

//loops until null is encountered and appends string c2 to c1

while (c2[k] != '\0') {

c1[i + k] = c2[k];

k++;

}

return c1;

}

int main() {

char string1[15] = "first";

char string2[15] = "second";

char *finalstr;

printf("Before concatenation:"

" \n string1 = %s \n string2 = %s", string1, string2);

//addresses of string1, string2 are passed to strct()

finalstr = strct(string1, string2);

printf("\nAfter concatenation:");

//prints the contents of string whose address is in finalstr

printf("\n finalstr = %s", finalstr);

//prints the contents of string1

printf("\n string1 = %s", string1);

//prints the contents of string2

printf("\n string2 = %s", string2);

return 0;

}

Output:

Before concatenation:

string1 = first

string2 = second

After concatenation:

finalstr = firstsecond

string1 = firstsecond

string2 = second

Explanation:

string2 is appended at the end of string1 and contents of string2 are unchanged. In strct() function, using a for loop, all the characters of string 'c2' are copied at the end of c1. return (c1) is equivalent to return &c1[0] and it returns the base address of 'c1'. 'finalstr' stores that address returned by the function strct().
answered by Geek (15.3k points) 17 40 61

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users