How to concatenate two strings without using strcat?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2007
Posts: 31
Reputation: IwalkAlone is an unknown quantity at this point 
Solved Threads: 0
IwalkAlone IwalkAlone is offline Offline
Light Poster

How to concatenate two strings without using strcat?

 
0
  #1
Mar 19th, 2007
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!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 9
Reputation: zigpy_siva is an unknown quantity at this point 
Solved Threads: 4
zigpy_siva's Avatar
zigpy_siva zigpy_siva is offline Offline
Newbie Poster

Re: How to concatenate two strings without using strcat?

 
0
  #2
Mar 19th, 2007
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<str2;i++)
str1[l1+i]=str2[i];
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: How to concatenate two strings without using strcat?

 
0
  #3
Mar 19th, 2007
>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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: How to concatenate two strings without using strcat?

 
0
  #4
Mar 20th, 2007
  1. /* myconcat.c */
  2.  
  3. #include <stdio.h>
  4.  
  5. int main(void)
  6. {
  7. char first_s[] = "Hello";
  8. char second_s[] = "World";
  9. int i = 0;
  10. int x = 0;
  11.  
  12. char long_s[sizeof(first_s) + sizeof(second_s)];
  13.  
  14. while(first_s[i])
  15. {
  16. long_s[i] = first_s[i];
  17. ++i;
  18. }
  19. long_s[i] = ' ';
  20. ++i;
  21.  
  22. while(second_s[x])
  23. {
  24. long_s[i] = second_s[x];
  25. ++i;
  26. ++x;
  27. }
  28. long_s[i] = '\0'; /* don't forget the null */
  29.  
  30. printf(long_s);
  31. putchar('\n');
  32.  
  33. getchar();
  34. return 0;
  35. }


If you want to learn something more advanced, do read this little snippet by Dave Sinkula.

http://www.daniweb.com/code/snippet406.html
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: IwalkAlone is an unknown quantity at this point 
Solved Threads: 0
IwalkAlone IwalkAlone is offline Offline
Light Poster

Re: How to concatenate two strings without using strcat?

 
0
  #5
Mar 21st, 2007
Hey I got it..i did it differently though..thnx neways
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 9
Reputation: zigpy_siva is an unknown quantity at this point 
Solved Threads: 4
zigpy_siva's Avatar
zigpy_siva zigpy_siva is offline Offline
Newbie Poster

Re: How to concatenate two strings without using strcat?

 
0
  #6
Mar 22nd, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1
Reputation: saswata has a little shameless behaviour in the past 
Solved Threads: 0
saswata saswata is offline Offline
Newbie Poster

Re: How to concatenate two strings without using strcat?

 
-2
  #7
Feb 3rd, 2009
Originally Posted by zigpy_siva View Post
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);
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 32
Reputation: Ahmed_I is an unknown quantity at this point 
Solved Threads: 1
Ahmed_I Ahmed_I is offline Offline
Light Poster

Re: How to concatenate two strings without using strcat?

 
0
  #8
Feb 3rd, 2009
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 .
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: How to concatenate two strings without using strcat?

 
0
  #9
Feb 3rd, 2009
Originally Posted by saswata View Post
#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);
}
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.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 15123 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC