String concatenation without strcat in C

I'm having problems concatenating strings in C, without the strcat library function. Here is my code

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char *a1=(char*)malloc(100); strcpy(a1,"Vivek"); char *b1=(char*)malloc(100); strcpy(b1,"Ratnavel"); int i; int len=strlen(a1); for(i=0;i<strlen(b1);i++) { a1[i+len]=b1[i]; } a1[i+len]='\0'; printf("\n\n A: %s",a1); return 0; } 

I entered corrections in the code. It works. Now can I do this without strcpy?

+4
source share
7 answers

Old answer below


You can initialize the string with strcpy , as in your code, or directly when declaring a char array.

 char a1[100] = "Vivek"; 

Other than that, you can do this char -by- char

 a1[0] = 'V'; a1[1] = 'i'; // ... a1[4] = 'k'; a1[5] = '\0'; 

Or you can write a few lines of code that replace strcpy and make them a function, or use directly in your main function.


Old answer

You have

  0 1 2 3 4 5 6 7 8 9 ...
     a1 [V | i | v | e | k | 0 | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _]
     b1 [R | a | t | n | a | v | e | l | 0 | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _ | _]

and you want

  0 1 2 3 4 5 6 7 8 9 ...
     a1 [V | i | v | e | k | R | a | t | n | a | v | e | l | 0 | _ | _ | _ | _ | _ | _ | _ | _]

So...

 a1[5] = 'R'; a1[6] = 'a'; // ... a1[12] = 'l'; a1[13] = '\0'; 

but with loops etc., right ?: D

Try this (don't forget to add the missing bits)

 for (aindex = 5; aindex < 14; aindex++) { a1[aindex] = b1[aindex - 5]; } 

Now think about 5 and 14 in the loop above.

What can you replace? When you answer this, you have solved the programming problem that you have :)

+4
source
  char a1[] = "Vivek"; 

Creates a char a1 array of size 6 . You are trying to write it with more characters than it can hold.

If you want to place the concatenation of "Vivek" and "Ratnavel" , you need to have an array of size char at least 14 (5 + 8 + 1) .

In your modified program:

 char *a1=(char*)malloc(100); // 1 a1 = "Vivek"; // 2 

1: allocates a piece of memory 100 bytes in size, forces a1 to point to it.
2: Set a1 to the string literal "Vivek" . This string literal cannot be changed.

To fix this, use strcpy to copy the string to the allocated memory:

 char *a1=(char*)malloc(100); strcpy(a1,"Vivek"); 

Also, the condition of the for i<strlen(b1)-1 loop will not copy the last character from the string, change it to i<strlen(b1)

AND

 a1[i]='\0'; 

it should be

 a1[i + len]='\0'; 

since the new length a1 is i+len and you need to have a NUL character in this index.

And don't forget the free dynamically allocated memory when you are done with it.

+4
source

You cannot write safely to these arrays since you have not made sure that enough space is available. If you use malloc() to allocate space, you cannot overwrite the pointer by assigning a string literal. You must use strcpy() to copy the string to the new allocated buffers. In this case.

In addition, the string length in C is calculated using the strlen() function, not the length() you use.

When concatenating, you need to stop working in the right place, which your code does not seem to be doing.

Here is how I could strcat() if needed for some reason:

 char * my_strcat(char *out, const char *in) { char *anchor = out; size_t olen; if(out == NULL || in == NULL) return NULL; olen = strlen(out); out += olen; while(*out++ = *in++) ; return anchor; } 

Note that this is as bad as strcat() when it comes to buffer overflows, since it does not support the limitation of the space used in the output, it just assumes that there is enough free space.

+1
source

Problems:

  • length not a function. strlen is, but you probably shouldn't call it in a loop - the length of b1 will not change for us, right? In addition, it returns size_t , which may be the same size as int on your platform, but it will be unsigned. This may (but usually will not) cause errors, but you should still be doing it right.
  • a1 has enough space for the first line, because the compiler does not know how to allocate additional stack space for the rest of the line. If you specify an explicit size, such as [100] , this should be enough for your purposes. If you need reliable code that doesn’t make any assumptions that β€œenough”, you should study malloc and friends, although this may be a lesson for another day.
  • Your loop stops too soon. i < b1_len (if you have a variable, b1_len , which was set to length b1 before the start of the loop), it would be enough - strlen does not take into account '\0' at the end.
  • But speaking of counting '\0' at the end, a slightly more efficient implementation could use sizeof a1 - 1 instead of strlen(a1) in this case, since a1 (and b1 ) are declared as arrays, not pointers. This is your choice, but remember that sizeof will not work for pointers, so don't confuse them. EDIT: New issues:
  • char *p = malloc(/*some*/); p = /* something */ char *p = malloc(/*some*/); p = /* something */ is the problem. = with pointers does not copy the contents, it copies the value, so you throw away the old pointer value that you received from malloc . To copy the contents of a string to char * (or char [] ), you need to use strcpy , strncpy or (my preference) memcpy . (Or just a loop, but this is pretty stupid. And again, this can be good practice if you write your own strcat .)
  • If you are not using C ++, I would not choose the return value of malloc , but this is a religious war, and we do not need one of them.
  • If you have strdup , use it. If you do not, do a working implementation:

     char *strdup(const char *c) { size_t l = strlen(c); char *d = malloc(l + 1); if(d) memcpy(d, c, l + 1); return d; } 

    This is one of the most useful features not in the C standard library.

+1
source

You can do this with strcpy () function too;)

 char *a = (char *) malloc(100); char *b = (char *) malloc(100); strcpy(a, "abc"); // initializes a strcpy(b, "def"); // and b strcpy((a + strlen(a)), b); // copy b at end of a printf("%s\n",a); // will produce: "abcdef" 
+1
source

I think this is easy.

 #include<stdio.h> int xstrlen(char *); void xstrcat(char *,char *,int); void main() { char source[]="Sarker"; char target[30]="Maruf"; int j=xstrlen(target); xstrcat(target,source,j); printf("Source String: %s\nTarget String: %s",source,target); } int xstrlen(char *s) { int len=0; while(*s!='\0') { len++; s++; } return len; } void xstrcat(char *t,char *s,int j) { while(*t!='\0') { *t=*t; t++; } while(*s!='\0') { *t=*s; s++; t++; } } 
0
source

It is better to split the strcat logic into a separate function. If you use pointer arithmetic, you don't need the strlen function:

 #include <stdio.h> #include <stdlib.h> #include <string.h> /* To completely get rid of this, implement your our strcpy as well */ static void my_strcat (char* dest, char* src) { while (*dest) ++dest; while (*src) *(dest++) = *(src++); *dest = 0; } int main() { char* a1 = malloc(100); char* b1 = malloc(100); strcpy (a1, "Vivek"); strcpy (b1, " Ratnavel"); my_strcat (a1, b1); printf ("%s\n", a1); /* => Vivek Ratnavel */ free (a1); free (b1); return 0; } 
-1
source

Source: https://habr.com/ru/post/1332088/


All Articles