Reputation: 17
I have a function that mixes 2 strings in specific way, and makes a new string, how can I allocate the memory of the new string?
I have tried adding this code but I'm not sure how to use it into a string that I don't know.
#include <stdio.h>
#include <string.h>
char *CreateString(char *string1, char *string2) {
int size;
int size1 = 0;
int size2 = 0;
for (int i = 0; *(string1 + i); i++) {
size1++;
}
for (int i = 0; *(string2 + i); i++) {
size2++;
}
size = (size1 * size2) + size1;
char string[size];
char *stringptr = string;
for(int i = 0; i < size; i = i + size2 + 1) {
*(stringptr + i) = *(string1++);
for (int j = 0; j < size2; j++) {
*(stringptr + i + j + 1) = *(string2 + j);
}
}
puts(string);
}
int main() {
char string1[] = "chocolate";
char string2[] = "123";
CreateString(string1, string2);
return 0;
}
Upvotes: 0
Views: 1173
Reputation: 3699
When I run your code, I get the string:
c123h123o123c123o123l123a123t123e123
It seems you want to add string2
to after each character of string2
.
So, in your case, you can call malloc
:
size = (size1 * size2) + size1;
char *string = malloc(size + 1); // +1 for null character
if(!string) {
printf("cannot malloc for string\n");
return NULL;
}
You function should return string
at the end. Do not forget to free the return value of this function when you do not want to use string
to avoid memory leak.
This code below:
for(int i=0;*(string1+i);i++){
size1++;
}
for(int i=0;*(string2+i);i++){
size2++;
}
Can be replaced by strlen
function:
size1 = strlen(string1);
size2 = strlen(string2);
Upvotes: 1
Reputation: 23208
Given your description, [from comments,given: "chocolate" "123" as user inputs]
"...string is made like this: c123o123c123o123l123a123t123e123 but with strings that i get from user."
CreateString
could be as simple as this:
char * CreateString(const char* string1,const char* string2)
{
int len1 = strlen(string1);
int len2 = strlen(string2);
char *ptr = string1;
char tempBuf[len1 + len1*len2 +1];// intermediate content buffer
char *newString = calloc(len1 + len1*len2 +1, 1);
if(newString)//test return of malloc
{
while(*ptr != NULL)//while each character is not NULL, stay in loop
{//use combination of sprintf and strcat to interleave
//components of new string
sprintf(tempBuf, "%s%c", newString, *ptr);
strcat(tempBuf, string2);
sprintf(newString, "%s", tempBuf);
ptr++;//increment pointer to next char in string1
}
}
return newString; //calling function should always check for null before using
}
int main(int argc, char *argv[])//Use this signature rather than
{ //int main(void) to allow user input
if(argc != 3) //simple verification that command line contains 2 arguments
{
printf("%s\n", "Usage: prog.exe <string1> <string2>");
return 0;
}
char *newString = CreateString(argv[1],argv[2]);
if(newString)
{
printf( "Resulting string is: %s\n", newString);
free(newString); //free when finished
}
return 0;
}
With user inputs of "chocolate"
and "123"
, program returns:
Upvotes: 1
Reputation: 144695
You can allocate memory for the string with
char *stringptr = malloc(size + 1);
Note that you code can be simplified with string functions from <string.h>
:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *CreateString(const char *string1, const char *string2) {
size_t size1 = strlen(string1);
size_t size2 = strlen(string2);
size_t size = size1 * (size2 + 1);
char *stringptr = malloc(size + 1);
if (stringptr) {
for (size_t i = 0; i < size; i = i + size2 + 1) {
stringptr[i] = *string1++;
strcpy(stringptr + i + 1, string2);
}
}
return stringptr;
}
int main() {
char string1[] = "chocolate";
char string2[] = "123";
char *string3 = CreateString(string1, string2);
if (string3) {
printf("%s\n", string3);
free(string3);
} else {
printf("cannot allocate memory\n");
}
return 0;
}
Upvotes: 1
Reputation: 103
Use the strlen
routine of string.h
:
size_t size=strlen(string);
size_t size2=strlen(string2);
size_t total_size=(size*size2)+size;
//now you make a malloc of a string result ugual to total_size+1 due to the fact the string end with "/0" character.
char* string_final;
string_final=malloc(sizeof(char)*(total_size+1));
if(string_final==NULL){
perror("Error in malloc");
}
else{
//...continue
}
Upvotes: 1