The task is to make 2 different functions. 1 where malloc creates a length for char and put in their 2 Strings. The other function can't give a return type. It should change the lengh of the 2 fused Strings to fit in a 3rd one. But in some it doesn't give the results like it should. The first "Failure" should be one but the other 3 once shouldn't be one...
#include <stdlib.h>
#include <string.h>
char *concatStrings( char* concaStr,char*str);
void concatStrings2( char* newString,char*str);
void testConcatStrings(char* concaStr,char*str, char*str2, char*expected);
int main()
{
char *str="Dynamic";
testConcatStrings("Memory", str,"Management","DynamicMemoryManagement");
testConcatStrings("Memory ", str,"Management","DynamicMemoryManagement");
testConcatStrings("Memory", str,"","DynamicMemory");
testConcatStrings("Memory","" ,"Management","MemoryManagement");
testConcatStrings("", str,"Management","DynamicManagement");
testConcatStrings("Memory", "","","Memory");
testConcatStrings("", str,"",str);
testConcatStrings("", "","Management","Management");
testConcatStrings("", "","","");
return 0;
}
char *concatStrings( char* concaStr,char*str){
//calculate suze for memory...
int lenStr=strlen(str);
int lenConcaStr=strlen(concaStr);
//+1 byte for 0x00
int len=lenStr+lenConcaStr+1;
//reserve memory
char *result =malloc(len);
//concatenate string
memcpy( result,str,lenStr);
memcpy( result+lenStr,concaStr,lenConcaStr+1);
printf("Hoi");
return result;
}
void concatStrings2( char* newString,char*str){
//calculate suze for memory...
int lenStr=strlen(str);
int lenConcaStr=strlen(newString);
//+1 byte for 0x00
int len=lenStr+lenConcaStr+1;
//reserve memory
printf("
Len: %d",len);
newString=(char*)realloc(newString,len);
printf(" %s %s ",newString,str);
//concatenate string
memcpy(newString+lenConcaStr,str,lenStr+1);
printf(" %s %s ",newString,str);
}
void testConcatStrings(char* concaStr,char*str, char*str2, char*expected){
//concatenated string by dynamic memory management(alloc)
char *result = concatStrings(concaStr,str);
concatStrings2(result,str2);
printf("
str= %s, concat: %s, str2: %s--> result:%s -->", str,concaStr,str2,result);
//check result
(strcmp(result,expected)==0)?printf("Success"): printf("Failure");
//never forget free!!!!
free(result);
printf("
Is free
");
}
Results:
Hoi
Len: 24 DynamicMemory Management DynamicMemoryManagement Management
str= Dynamic, concat: Memory, str2: Management--> result:DynamicMemoryManagement -->Success
Is free
Hoi
Len: 25 DynamicMemory Management DynamicMemory Management Management
str= Dynamic, concat: Memory , str2: Management--> result:DynamicMemory Management -->Failure
Is free
Hoi
Len: 14 DynamicMemory DynamicMemory
str= Dynamic, concat: Memory, str2: --> result:DynamicMemory -->Success
Is free
Hoi
Len: 17 Memory Management MemoryManagement Management
str= , concat: Memory, str2: Management--> result:╚d -->Failure
Is free
Hoi
Len: 18 Dynamic Management DynamicManagement Management
str= Dynamic, concat: , str2: Management--> result: -->Failure
Is free
Hoi
Len: 7 Memory Memory
str= , concat: Memory, str2: --> result:Memory -->Success
Is free
Hoi
Len: 8 Dynamic Dynamic
str= Dynamic, concat: , str2: --> result:Dynamic -->Success
Is free
Hoi
Len: 11 Management Management Management
str= , concat: , str2: Management--> result:╚d -->Failure
Is free
Hoi
Len: 1
str= , concat: , str2: --> result: -->Success
Is free
(the results look so messy bc I tried to see exactly what happen in the functions and where exactly the error starts.)