954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to concatenate two strings without using strcat?

Q.Write a function to concatenate to input strings into one large string and display the result.

I know how to do this using strcat but not without it. Also can't use pointers. Please help!!

IwalkAlone
Light Poster
31 posts since Feb 2007
Reputation Points: 17
Solved Threads: 0
 

hi it is very easy find the length of the first character array, and find the length of the second character array, using a loop concodinate both strings.
i dont know it is correct or not

char str1[],str2[];
int l1,l2;

l1=length(str1);
l2=length(str2);

for(i=0 ;i

zigpy_siva
Newbie Poster
16 posts since Mar 2007
Reputation Points: 10
Solved Threads: 5
 

>i dont know it is correct or not
It's not. Why don't you test out thing on your compiler first before posting them?

To concatenate a string, remember that strings are basically arrays of characters. This means that you can loop through the arrays (or strings) and copy letters into another one. First of all, create an array that's the combined length of both strings (plus 1 for the terminating null character). Then simply start by copying the first string into the array, and then proceed by doing the same to the other.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 
/* myconcat.c */

#include <stdio.h>

int main(void)
{
    char first_s[] = "Hello";
    char second_s[] = "World";
    int i = 0;
    int x = 0;
    
    char long_s[sizeof(first_s) + sizeof(second_s)];

    while(first_s[i])
    {
        long_s[i] = first_s[i];
        ++i;
    }
    long_s[i] = ' ';
    ++i;
    
    while(second_s[x])
    {
        long_s[i] = second_s[x];
        ++i;
        ++x;
    }
    long_s[i] = '\0'; /* don't forget the null */
    
    printf(long_s);
    putchar('\n');
    
    getchar();
    return 0;
}

If you want to learn something more advanced, do read this little snippet by Dave Sinkula. http://www.daniweb.com/code/snippet406.html

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Hey I got it..i did it differently though..thnx neways :)

IwalkAlone
Light Poster
31 posts since Feb 2007
Reputation Points: 17
Solved Threads: 0
 

sorry i am now using vb very much but i have used c only 2 years befor. i was in a cafe so i was unable to compile the program...

zigpy_siva
Newbie Poster
16 posts since Mar 2007
Reputation Points: 10
Solved Threads: 5
 
sorry i am now using vb very much but i have used c only 2 years befor. i was in a cafe so i was unable to compile the program...
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
int l1,l2,i;
   clrscr();
gets(str1);
gets(str2);
l1=strlen(str1);
l2=strlen(str2);

for(i=0;i<=l2;i++)
 {
  str1[l1+i]=str2[i];
 }
 printf("%s",str1);
}
saswata
Newbie Poster
1 post since Feb 2009
Reputation Points: 1
Solved Threads: 0
 

Or u could use a Linked List to put the first string in a List and put the second string in another list .
Then u can link the two lists together to get A concatenated string :-O .

Ahmed_I
Light Poster
32 posts since Nov 2008
Reputation Points: 14
Solved Threads: 1
 

#include #include #include void main() { char str1[30],str2[30]; int l1,l2,i; clrscr(); gets(str1); gets(str2); l1=strlen(str1); l2=strlen(str2);

for(i=0;i<=l2;i++) { str1[l1+i]=str2[i]; } printf("%s",str1); }


The main function always returns an integer when hosted by an operating system. So void main() is not acceptable.
gets() is not acceptable neither, for different reason. It can not be stop of reading more that buffer can support, without overflow. #include<conio.h> and clrscr(); are none portable, therefore voids the "warranty". Furthermore, clearing the screen is a doubtful practice, for the sake of "prettiness".
Next time use the expected code tags to post source code. Here's an example .

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

#include #include #include void main() { char str1[30],str2[30]; int l1,l2,i; clrscr(); gets(str1); gets(str2); l1=strlen(str1); l2=strlen(str2);

for(i=0;i<=l2;i++) { str1[l1+i]=str2[i]; } printf("%s",str1); }


how if i want to add the string to the front??

ericccc
Newbie Poster
2 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Just swap every variable below the strlen() functions.
l1 <=> l2
str1 <=> str2

Also:
You should declare main as returning an int, not void.
And you should NOT use gets(), use fgets() instead.
Do some reading on why, it's quite interesting and important to know.

holocron
Newbie Poster
14 posts since Feb 2010
Reputation Points: 8
Solved Threads: 0
 

Here's the simple version of the program:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	char arr1[] = "Ashutosh";
	char arr2[] = " Thakur";
	cout<<"SizeOf 1st: "<<sizeof arr1<<endl; //Get the size of first string
	cout<<"SizeOf 2nd: "<<sizeof arr2<<endl; //Get the size of second string
	int size = (sizeof arr1 + sizeof arr2);
	cout<<"Size of 3rd: "<<size<<endl;
	char arr3[sizeof arr1 + sizeof arr2];  //size of third string
	for(int i=0;i<sizeof arr1;i++)		   //Run loop from 0 to size of 1st arr
	{
		arr3[i] = arr1[i];
	}
	for(int j=(sizeof arr1-1),k=0;j<17,k<sizeof arr2; j++,k++) //Run loop from size end loop to size of 3nd array 
	{														   //Run loop from 2nd array to last of its size
		arr3[j] = arr2[k];
	}

	for(int k=0;k<sizeof arr3; k++)
	{
		cout<<arr3[k];
	}
	system("pause");
}
ashutosh1612
Newbie Poster
1 post since Mar 2010
Reputation Points: 6
Solved Threads: 0
 
vaishali singh
Newbie Poster
1 post since Apr 2010
Reputation Points: 9
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You