473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

confused with strcat() :(

16
hello,
I am supposed to concatenate 2 strings (words). For this the function should call malloc or calloc to get memory for the newstring(after concatenated) .I am not allowed to use the library string.h. I figured out to find the length of the 2 strings. .. but i don't get how to concatenate the two words.
P.S This is the first time i am doing program with dynamic memory allocation.

so far this is my code:
Expand|Select|Wrap|Line Numbers
  1.  #include<stdio.h> 
  2. #include<stdlib.h>
  3.  
  4.  
  5. int my_strlen(char *string1, char *string2);
  6. //char*my_strcat(char *string1,char *string2);
  7.  
  8. main(){
  9.     char *string1 = "Hello";
  10.     char *string2 = "world!";
  11.  
  12.     my_strlen(string1,string2);
  13.  
  14. //    printf("\n %d %d",n[0],n[1]);//
  15.  
  16. printf("%s\n",my_strcat("Hello","world!"));
  17. }
  18.  
  19. int my_strlen(char *string1,char *string2){
  20. int i;
  21. int *newstring;
  22.     int *newstring1;
  23. newstring = malloc(sizeof(string1 +1));
  24.     newstring1= malloc(sizeof(string2 +1));
  25.  
  26.     if(newstring == 0|| newstring1==0)
  27.         printf("no memory");
  28.  
  29.     printf("%d %d", newstring,newstring1);
  30. /* for(i=0;*newstring!='\0',*newstring1!='\0';newstring++,newstring1++)
  31.         i++;*/
  32.     return (newstring,newstring);
  33. }
  34.  
  35. /*char *my_strcat(char*string1, char *string2){
  36. while(*string1) string1++;
  37. while(*string1++ = *string2++);
  38.  
  39.     return(string1,string2);
  40. }*/
  41. // warnings and error//
  42.  
warning C4013: 'my_strcat' undefined; assuming extern returning int
: warning C4047: 'return' : 'int ' differs in levels of indirection from 'int *'
: warning C4101: 'i' : unreferenced local variable
: error C2040: 'my_strcat' : 'char *(char *,char *)' differs in levels of indirection from 'int ()'
Error executing cl.exe.


I think i am making the problem more complicated than it is... i would like to know the concept behind dynamic memory allocation..
any help would be great!! thanks
Oct 6 '06 #1
1 3082
tyreld
144 100+
You say you figured out how to determine the string length, but the code you posted makes it look like you are trying to do the concatenation in the my_strlen function. Anyways, to concatenate two strings you obviously need to know the lengths of both so you know how much space you need for the new string.

1.) Your string length function would probably be better just taking one string as input and returning the length of that one string. The basic approach being to loop through the string counting each character up unitl the null byte ('\0'), and returning the final count.

2.) To concatenate take 2 strings as input. Determine the lenghts of both strings and add these 2 lengths together to get the overall length. Now add 1 to that overall length because you'll need room for the null byte in your new string. Allocate some memory for your new string.

malloc(sizeof(char) * (new_string_length + 1)) // plus one for the null byte

Now loop through the first string coping each character into the new string.

Now loop through the second string and copy each character to the new string (hint: use a standard 0->len of 2nd string for the loop, but when you access the new string use the loop counter + len of the first string to get the right index).

Finally, add the null byte to the end of the new string.
Oct 6 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Patrick Coleman | last post by:
Hi, I have the following code: char request = "GET "; strcat(request, path); //Path is the path section of a url ie. "/path/test.htm" strcat(request, " HTTP/1.1"); cout<<request<<"\n";...
9
by: Pascal Damian | last post by:
I read somewhere that strcpy() is safer when dealing with malloc()-ed strings. Is that true? (Of course I know that both are unsafe). -- Pascal
5
by: Kevin C. | last post by:
Never mind my last post about pointer subtraction, I traced the code and found the offender to be strcat. char call = "del "; system(strcat(strcat(call, del->table_name), ".tab")); After this...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
8
by: ctara_shafa | last post by:
Hi, I have a following problem: I'm creating a list and one of the fields should contain the date. Firstly I ask the user for the year, month and day and then I'd like to collect all this data in...
9
by: cicalese | last post by:
For starters, I am a C novice. I don't fully understand how strings work. I am running on a linux box. I compile w/ gcc. I have a string called 'myabqcommand' that is being overwritten when i use...
87
by: Robert Seacord | last post by:
The SEI has published CMU/SEI-2006-TR-006 "Specifications for Managed Strings" and released a "proof-of-concept" implementation of the managed string library. The specification, source code for...
4
by: nick048 | last post by:
Hi, I have this problem: int n; char nToChar; char firstString = "The number is: "; char resp; /* HERE MAIN WITH THE INPUT OF n*/
3
by: sail0r | last post by:
Perhaps this is obvious but I am not sure what is going on... Here is the relevant code: char *command; char *argument; char url="file:///usr/u/myname/Project/cats/"; char...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.