I had this working, but it was only inserting one word into a string at given length using scanf.
scanf only gets one word at a time. To eliminate that only one word limit unknown words wanted to be added to line thing. I replaced scanf with getline.
now I am having problems getting a new char length of line1 + line2 + 1 using the method I used then went to malloc and still am getting a 0 (zero) on length using malloc to further modify the code to add more than one word into a string at given position.
( some comments in code to explaine as well )
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * insertString(char* destination, int pos, char* sub_string)
{
printf("Destanation %s\n", destination);
// gets 45 length
printf("\nstrlen Destanation %lu\n\n", strlen(destination) );
// gets whatever lenght input is
printf("\nstrlen sub_string %lu\n\n", strlen(sub_string) );
// add the two together plus one for \0
// char HoldStr1[ (strlen(destination))+(strlen(sub_string))+1 ];
//add the two together plus 1
// gets 6
printf("\n\nstrlen HoldStr1 %lu\n\n", strlen(HoldStr1) );
//do it this way
int size_of_string = (strlen(destination))+(strlen(sub_string))+1;
// gets 58 which = dest len + whatever input len is + 1
printf("size of string %d\n",size_of_string );
// do it this way gets zero lenght
//char *HoldStr = (char *)malloc(size_of_string * sizeof(char));
// do it this way size_of_string = 58
char *HoldStr;
HoldStr = (char *)malloc(size_of_string);
//gets zero
printf("\nstrlen HoldStr %lu\n\n", strlen(HoldStr) );
// cannot go any further until above is fixed.
// it now just returns the sub_string input as output
// like this
strncpy(HoldStr,destination,pos);
printf("1. HoldStr %s\n",HoldStr);
HoldStr[pos] = '\0';
strcat(HoldStr,sub_string);
printf("2. HoldStr %s\n",HoldStr);
strcat(HoldStr,destination+pos);
printf("3. HoldStr %s\n",HoldStr);
char *return_new_string;
return_new_string = (char *)malloc(strlen(HoldStr)+1);
strcpy(return_new_string,HoldStr);
return return_new_string;
}
int main (void)
{
char *string1 = "This is base string to insert something into.";
//char string1[51];
size_t buffer_size = 51;
size_t charsAre;
char *string2;
//int ch;
int insert = 0;
string2 = (char *)malloc(buffer_size * sizeof(char));
if ( string2 == NULL)
{
printf("Can't get buffer\n");
exit(1);
}
printf("IN MAIN: StrLen %lu\n", strlen(string1) );
// printf("Enter your first string. No more than 50 chars long\n");
//scanf("%s", string1);
printf("enter a short string 50 max length,\nand a number where to put it too\n");
// ch = getchar(); // to try to stop scanf from skipping
//scanf("%s %d", string2, &insert);
charsAre = getline(&string2, &buffer_size, stdin);
printf("input charsAre %zu\n", charsAre);
printf("IN MAIN str2: StrLen %lu\n", strlen(string2) );
printf("String2 = %s\n", string2);
//output
char *retur = insertString(string1, insert, string2);
printf("Returned String: main: %s \n", retur);
return 0;
}
if I go back to using just scanf it works just fine like that, but only one word again.
I didn't start looking at strlen until after I started trying to expand on only one word inserts. So I must be missing something.
Code:
destination string
This is base string to insert something into.
input string
hello you 5
output
Returned String: main: This ihellos base string to insert something into.