how add space between two words in array ,i receive two words from the user his first name ,his last name what i need it to store the two arrays in one array between them a space
here is my code
#include <stdio.h>
int main()
{
char first_name[15], last_name[15] ,full_name[32];
int i=0,i2,first_name_length,last_name_length;
printf("enter your first name\n");
first_name_length = scanf("%s",first_name);
printf("enter your last name\n");
last_name_length = scanf("%s",last_name);
for(i2 = 0;i2 < first_name_length;i2++){
full_name[i] = first_name[i2];
i++;
}
full_name[i++]=' ';
for(i2 = 0;i2 < last_name_length;i2++){
full_name[i] = last_name[i2];
i++;
}
printf("%s",full_name);
return 0;
}
the output when enter the "name" value in the both scanf :
n n
and it should be :
name name
edit:
#include <stdio.h>
int main()
{
char first_name[15], last_name[15] ,full_name[32];
int i=0,i2;
printf("enter your first name\n");
scanf("%14s",first_name);
printf("enter your last name\n");
scanf("%14s",last_name);
for(i2 = 0;first_name[i2];i2++){
full_name[i] = first_name[i2];
i++;
}
full_name[i++]=' ';
for(i2 = 0;last_name[i2];i2++){
full_name[i] = last_name[i2];
i++;
}
printf("%s",full_name);
return 0;
}
the output when enter the "tom" value in the first scanf and "fox" value in the second scanf :
tom fox↓@
and it should be :
tom fox
first_nameandlast_nameare 15 char each andfull_nameis 30. If you add a space between a max length first_name and max length last_name, you're going to be in trouble.iafter you set the space, you're incrementing it before hand.