How to concatenate strings without using a function?

Asked

Viewed 1,148 times

-1

Good afternoon guys, I have this question. How do I concatenate two strings without using function or library for this?

  • I don’t think there’s a way. As far as I know, the C language unfortunately isn’t like PHP or JS that a + or . between 2 strings generates concatenation.

  • C strings are just char arrays (characters). If I’m not mistaken, you cannot directly concatenate strings into C, you need to use the function strcat() for that reason.

  • Someone voted against the question and all the answers. I could explain?

3 answers

3


You create a new string of sufficient size and copy the characters of the two strings into the new one. The size is the sum of the sizes of the other two strings. The copy of each string can be done with a for for each.

That is to say:

char a[] = "teste1";
char b[] = "teste2";

int tamanho1 = 0;
while (a[tamanho1]) tamanho1++;

int tamanho2 = 0;
while (b[tamanho2]) tamanho2++;

int tamanho3 = tamanho1 + tamanho2 + 1;
char *c = (char *) malloc(tamanho3);

for (int i = 0; a[i]; i++) {
    c[i] = a[i];
}

for (int i = 0; b[i]; i++) {
    c[i + tamanho1] = b[i];
}

c[tamanho1 + tamanho2] = 0;

See here working on ideone.

However, I don’t recommend doing that. The tendency to do this is to reinvent the wheel multiple times, which is unnecessary, confusing and very prone to errors. The function strlen replaces these two whiles. The function of strcat could be used to eliminate fors.

  • It helped me a lot, thanks Victor!! I now understand the idea of how to cocatenate the strings.

  • @Adrielgama If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment. :)

1

There are several ways to concatenate strings. Here are some examples that are more practical and less costly.

char * str1 = "Teste1";
char * str1 = "Teste2";
char str3[50];

/* Exemplo 1 */
memcpy(str3, str1, 6);
memcpy(str3[6], str2, 6); // str3: "Teste1Teste2"  

/* Exemplo 2 */
memcpy(str3, str1, 6);
memcpy(str3[6], "", 1);
memcpy(str3[7], str2, 6); // str3: "Teste1 Teste2"
  • To get the size of the array you can also use: sizeof(char) / sizeof(str1[0]);

-1

You can iterate through the character array of the second string and add one to one at the end of the first string.

char str1[100], str2[100], i, j;
//Conta o tamanho da primeira string (poderia usar strlen) - \0 é o fim da string

for(i=0; str1[i]!='\0'; ++i); 

// O segundo loop concatena cada caracter da segunda string ao fim da primeira. 
// Teste antes para não estourar o tamanho máximo da primeira.

for(j=0; str2[j]!='\0'; ++j, ++i) {
  str1[i]=str2[j];
}

str1[i]='\0';

I don’t know why you would use something like this, since you would have to implement all the controls again, but that’s the way it is.

  • Professor asked to generate an automatic email from just one entry containing the person’s full name, using strings. The idea is to take the first name, the last name, put a dot between the two and print it with the @domain you choose. But all this without using any function.

  • I get it, it’s purely didactic. This very basic way that I answered would work, as long as you concatenate the "." point between the strings (names) and add the domain later. It’s the same logic. Did you really understand the answer you marked as right? It uses a function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.