There are several methods for concatenating strings in C, you can use sprinf
, or strcat
.
The sprintf
, works equal to printf
, the difference is the parameter referring to the string that you will enter the value.
You can concatenate numbers and floating points using sscanf
.
strcat:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv){
char str1[50];
char str2[50];
char cat[100];
strcpy(str1,"texto da string1"); // insere o texto em str
strcpy(str2," | nova parte do texto");
bzero(cat, 100); // limpa a variavel cat
strcat(cat, str1); // concatena valores em cat
strcat(cat, str2);
puts(cat);
}
sprintf:
#include <stdio.h>
int main(int argc, char **argv){
char str1[50];
char str2[50];
char cat[100];
sprintf(str1,"Primeira parte");
sprintf(str2,"Segunda parte");
sprintf(cat,"%s - %s",str1, str2);
puts(cat);
}