Concatenate chars/strings using a function

I am trying to solve an exercise consisting in getting a string with a full name considering 3 strings as follow:
String1: Name
String2: Last Name 1
String3: Last Name 2

The result must be:
String4: Last Name1 Last Name2, Name

My code prints nothing. This is my work up to now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void full_name(char *p_name, char *p_lastname1, char *p_lastname2, char *p_finalname) {
    int i=0, j=0;
    while(p_lastname1[j]){
       p_finalname[i++]=p_lastname1[j++];
    }
    j=0;
    while(p_lastname2[j]) {
        p_finalname[i++]=p_lastname2[j++];
    }
    while(p_name[j]){
        p_finalname[i++]=p_name[j++];
    }
}
int main()
{
    char name[1024], lastname1[1024], lastname2[1024], finalname[1024];
    char *p_name=&name[0];
    char *p_lastname1=&lastname1[0];
    char *p_lastname2=&lastname2[0];
    char *p_finalname=&finalname[0];

    printf("Insert the name: ");
    scanf("%s", p_name);

    printf("Insert the first last name: ");
    scanf("%s", p_lastname1);

    printf("Insert the second last name: ");
    scanf("%s", p_lastname2);

    printf("The complete name is %s",p_finalname);
    full_name(p_name,p_lastname1,p_lastname2,p_finalname);

    return 0;
}


I am trying to use pointers from arrays for every text (string). The idea is concatenate three texts: name, lastname1, lastname2 in a resulting string: finalname creating a function to concatenate strings and printing the result in int main (). Could you help me?

Thank you in advance.
Last edited on
If you use C++ strings, you can concatenate them easily.

1
2
3
string first("Beans ");
string second("On Toast");
string third = first + second; // third is the concatenation, "Beans On Toast" 


C++ strings exist for a reason.
If you are stuck with C-strings:

You try and print the result on line 37 ... before you work it out on line 38. (Always walk through the code line by line.)

You need another
j=0;
between lines 15 and 16.

You need to null-terminate your C-string before leaving void full_name():
p_finalname[i]='\0';


If these are names then you might want to add spaces or commas between them.

You don't need the pointers in main(): just use the name of the array.
1
2
3
4
5
6
7
8
9
10
11
    printf("Insert the name: ");
    scanf("%s", name);

    printf("Insert the first last name: ");
    scanf("%s", lastname1);

    printf("Insert the second last name: ");
    scanf("%s", lastname2);

    full_name(name,lastname1,lastname2,finalname);
    printf("The complete name is %s",finalname);
Last edited on
Great! Is almost done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void full_name(char *p_name, char *p_lastname1, char *p_lastname2, char *p_finalname) {
    int i=0, j=0;
    while(p_lastname1[j]){
       p_finalname[i++]=p_lastname1[j++];
    }
    j=0;
    while(p_lastname2[j]) {
        p_finalname[i++]=p_lastname2[j++];
    }
    j=0;
    while(p_name[j]){
        p_finalname[i++]=p_name[j++];
    }
   p_finalname[i]='\0';
}
int main()
{
    char name[1024], lastname1[1024], lastname2[1024], finalname[1024];
    char *p_name=&name[0];
    char *p_lastname1=&lastname1[0];
    char *p_lastname2=&lastname2[0];
    char *p_finalname=&finalname[0];

    printf("Insert the name: ");
    scanf("%s", p_name);

    printf("Insert the first last name: ");
    scanf("%s", p_lastname1);

    printf("Insert the second last name: ");
    scanf("%s", p_lastname2);

    full_name(p_name,p_lastname1,p_lastname2,p_finalname);
    printf("The complete name is %s",p_finalname);

    return 0;
}


This is what I get with that code:

Insert the name: George
Insert the first last name: Reynolds
Insert the second last name: Banks
The full name is BanksReynoldsGeorge

I only need that the output gives me: Banks Reynolds, George

How can I put the spaces and "," to format the result as this?

Thank you!

Edited: I had j=0 between lines 15 and 16 but I translated the code to english so accidentally I removed it in the forum, sorry.
Last edited on
Moschops, thanks for the tip. I didn't know, I will try later.
How can I put the spaces and "," to format the result as this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void full_name(char *p_name, char *p_lastname1, char *p_lastname2, char *p_finalname) {
    int i=0, j=0;
    while(p_lastname1[j]){
       p_finalname[i++]=p_lastname1[j++];
    }
    p_finalname[i++]=' ';                         // <=== add a space
    j=0;
    while(p_lastname2[j]) {
        p_finalname[i++]=p_lastname2[j++];
    }
    p_finalname[i++]=',';                         // <=== add a comma
    p_finalname[i++]=' ';                         // <=== add a space
    j=0;
    while(p_name[j]){
        p_finalname[i++]=p_name[j++];
    }
    p_finalname[i]='\0';
}



Note that your headers could be more consistently in the modern form:
1
2
3
#include <iostream>
#include <cstdio>
#include <cstdlib> 



This is a decent exercise to teach you problem solving, pointers, arrays etc.
Note, however, that if you are using C-strings then you can circumvent much of this with strcat().
And, as others have pointed out, using the more modern string class makes life a lot easier ...


I've seen your other post in the lounge section. Don't despair - it's a good decision to get extra qualifications and, as I see first-hand that (in England at least) programming languages are disappearing from university engineering curricula nearly as fast as European languages are disappearing from A-level provision, your computing skills will stand you in good stead in an engineering career.
Thank you lastchance! I have the program running perfectly. I will put the code in case someone else need it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void full_name(char *p_name, char *p_lastname1, char *p_lastname2, char *p_finalname) {
    int i=0, j=0;
    while(p_lastname1[j]){
       p_finalname[i++]=p_lastname1[j++];
    }
    p_finalname[i++]=' ';    //Adds a space
    j=0;
    while(p_lastname2[j]) {
        p_finalname[i++]=p_lastname2[j++];
    }
    p_finalname[i++]=', ';   //Adds a comma
    p_finalname[i++]='  ';   //Adds a space
    j=0;
    while(p_name[j]){
        p_finalname[i++]=p_name[j++];
    }
   p_finalname[i]='\0';
}
int main()
{
    char name[1024], lastname1[1024], lastname2[1024], finalname[1024];
    char *p_name=&name[0];
    char *p_lastname1=&lastname1[0];
    char *p_lastname2=&lastname2[0];
    char *p_finalname=&finalname[0];

    printf("Insert the name: ");
    scanf("%s", p_name);

    printf("Insert the first last name: ");
    scanf("%s", p_lastname1);

    printf("Insert the second last name: ");
    scanf("%s", p_lastname2);

    full_name(p_name,p_lastname1,p_lastname2,p_finalname);
    printf("The complete name is %s",p_finalname);

    return 0;
}


I really appreciate your words, I know if I keep on working hard, someday I will have a job again either in Spain or any other country. Thank you very much for your inspirational message, is very welcome in those hard times, a light in the dark.

You made my day! and the problem was perfectly solved.
Topic archived. No new replies allowed.