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: - #include<stdio.h>
-
#include<stdlib.h>
-
-
-
int my_strlen(char *string1, char *string2);
-
//char*my_strcat(char *string1,char *string2);
-
-
main(){
-
char *string1 = "Hello";
-
char *string2 = "world!";
-
-
my_strlen(string1,string2);
-
-
// printf("\n %d %d",n[0],n[1]);//
-
-
printf("%s\n",my_strcat("Hello","world!"));
-
}
-
-
int my_strlen(char *string1,char *string2){
-
int i;
-
int *newstring;
-
int *newstring1;
-
newstring = malloc(sizeof(string1 +1));
-
newstring1= malloc(sizeof(string2 +1));
-
-
if(newstring == 0|| newstring1==0)
-
printf("no memory");
-
-
printf("%d %d", newstring,newstring1);
-
/* for(i=0;*newstring!='\0',*newstring1!='\0';newstring++,newstring1++)
-
i++;*/
-
return (newstring,newstring);
-
}
-
-
/*char *my_strcat(char*string1, char *string2){
-
while(*string1) string1++;
-
while(*string1++ = *string2++);
-
-
return(string1,string2);
-
}*/
-
// warnings and error//
-
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
1 3082
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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";...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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*/
|
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...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |