943,715 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 53635
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 19th, 2007
0

How to concatenate two strings without using strcat?

Expand Post »
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!!
Similar Threads
Reputation Points: 17
Solved Threads: 0
Light Poster
IwalkAlone is offline Offline
31 posts
since Feb 2007
Mar 19th, 2007
0

Re: How to concatenate two strings without using strcat?

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];
Reputation Points: 10
Solved Threads: 5
Newbie Poster
zigpy_siva is offline Offline
16 posts
since Mar 2007
Mar 19th, 2007
0

Re: How to concatenate two strings without using strcat?

>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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 20th, 2007
0

Re: How to concatenate two strings without using strcat?

  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
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Mar 21st, 2007
0

Re: How to concatenate two strings without using strcat?

Hey I got it..i did it differently though..thnx neways
Reputation Points: 17
Solved Threads: 0
Light Poster
IwalkAlone is offline Offline
31 posts
since Feb 2007
Mar 22nd, 2007
0

Re: How to concatenate two strings without using strcat?

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...
Reputation Points: 10
Solved Threads: 5
Newbie Poster
zigpy_siva is offline Offline
16 posts
since Mar 2007
Feb 3rd, 2009
-3

Re: How to concatenate two strings without using strcat?

Click to Expand / Collapse  Quote originally posted by zigpy_siva ...
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...



  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. void main()
  5. {
  6. char str1[30],str2[30];
  7. int l1,l2,i;
  8. clrscr();
  9. gets(str1);
  10. gets(str2);
  11. l1=strlen(str1);
  12. l2=strlen(str2);
  13.  
  14. for(i=0;i<=l2;i++)
  15. {
  16. str1[l1+i]=str2[i];
  17. }
  18. printf("%s",str1);
  19. }
Last edited by Nick Evan; Feb 10th, 2010 at 1:33 pm. Reason: Added code-tags
Reputation Points: 1
Solved Threads: 0
Newbie Poster
saswata is offline Offline
1 posts
since Feb 2009
Feb 3rd, 2009
-1

Re: How to concatenate two strings without using strcat?

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 .
Reputation Points: 14
Solved Threads: 1
Light Poster
Ahmed_I is offline Offline
32 posts
since Nov 2008
Feb 3rd, 2009
0

Re: How to concatenate two strings without using strcat?

Click to Expand / Collapse  Quote originally posted by saswata ...
#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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Feb 10th, 2010
0

add to front

Click to Expand / Collapse  Quote originally posted by saswata ...
#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);
}
how if i want to add the string to the front??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ericccc is offline Offline
2 posts
since Feb 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: store each line of a file to a char pointer
Next Thread in C Forum Timeline: Write this without lib and string.h files help me plz





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC