1

My code is unable to print the full name, it is just printing out the first name. I'm a little rookie in this. This is my output:

enter image description here

Here is the code

#include <stdio.h>

int main()
{
    int i, j, k;
    char fName[15], mName[15], lName[15], name[45];
    
    printf("\nEnter the First name: ");
    scanf("%s", fName);
    printf("\nEnter the Middle name: ");
    scanf("%s", mName);
    printf("\nEnter the Last name: ");
    scanf("%s", lName);

    for (i = 0; i < 15; i++) {
        name[i] = fName[i];
        if (fName[i] == '\0') {
            i++;
            break;
        }
    }

    for (j = 0; j < 15; j++) {
        name[i + j] = mName[j];
        if (mName[j] == '\0') {
            j++;
            break;
        }
    }
    for (k = 0; k < 15; k++) {
        name[i + j + k] = lName[k];
        if (lName[k] == '\0') {
            k++;
            break;
        }
    }

    printf("\nFull name is %s.", name);

    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • You should stop before the `\0`, not after it. Or, don't increment `i` after appending the `\0`, because then printf sees that and thinks it's the end of the string. – Andy Turner May 22 '21 at 07:22
  • 2
    When using `scanf()` to fill an array, you must ALWAYS provide a ***width-modifier*** to prevent writing beyond the end of your array, e.g. `scanf ("%14s", fName);`. Otherwise, the use of `scanf()` with `"%s"` is no safer than using `gets()`. See [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) – David C. Rankin May 22 '21 at 08:07
  • Are you not allowed to use the handy C standard library functions that manipulate character strings? As a general principle, *all* new code is buggy for the first few iterations, which is why we should use the standard functions, which have left this phase behind since ca. 1982 ;-). If you can use them, look up strcpy and strcat. – Peter - Reinstate Monica May 22 '21 at 16:10

3 Answers3

1

Break out of the copy loop when encountering a '\0' char. In your code, the '\0' is being copied to the output, which is then being interpreted as the end of string, and in your case at the end of the first name. However, for the last name, you need to include the \0 char.

#include <stdio.h>
int main()
{
    int i, j, k;
    char fName[15], mName[15], lName[15], name[45];
    printf("\nEnter the First name: ");
    scanf("%s", fName);
    printf("\nEnter the Middle name: ");
    scanf("%s", mName);
    printf("\nEnter the Last name: ");
    scanf("%s", lName);

    for (i = 0; i < 15; i++)
    {
        if (fName[i] == '\0')
        {
            i++;
            break;
        }
        name[i] = fName[i];
    }

    for (j = 0; j < 15; j++)
    {
        if (mName[j] == '\0')
        {
            j++;
            break;
        }
        
        name[i + j] = mName[j];
    }
    for (k = 0; k < 15; k++)
    {
        name[i + j + k] = lName[k];
        if (lName[k] == '\0')
        {
            k++;
            break;
        }
    }
    
    printf("\nFull name is %s.", name);

    return 0;
}
Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79
  • See comment under original question about using `"%s"` without a *width-modifier*. – David C. Rankin May 22 '21 at 08:07
  • Your remark is correct: the null byte should not be copied, but your code still increments `i`, `j` and `k` leaving the corresponding element of `name` uninitialized, which leads to undefined behavior. – chqrlie May 22 '21 at 12:11
1

In your code, you copy the null terminator of each string into the name array, hence the final contents is "Alexander\0Graham\0Bell". printf stops at the first null byte it finds in the string argument for %s so only the first name is output.

You should instead use a separate index for the source and destination arrays and set a space between the 3 parts of the name.

Note also that you should tell scanf() to stop reading after 14 bytes to avoid storing characters beyond the end of the arrays on overlong user input and test the return value to detect invalid input.

Here is a modified version:

#include <stdio.h>

int main() {
    char fName[15], mName[15], lName[15], name[45];
    int i, j;
    
    printf("\nEnter the First name: ");
    if (scanf("%14s", fName) != 1)
        return 1;
    printf("\nEnter the Middle name: ");
    if (scanf("%14s", mName) != 1)
        return 1;
    printf("\nEnter the Last name: ");
    if (scanf("%14s", lName) != 1)
        return 1;

    i = 0;
    for (j = 0; fName[j] != '\0'; j++) {
        name[i++] = fName[j];
    }
    name[i++] = ' ';
    for (j = 0; mName[j] != '\0'; j++) {
        name[i++] = mName[j];
    }
    name[i++] = ' ';
    for (j = 0; lName[j] != '\0'; j++) {
        name[i++] = lName[j];
    }
    name[i] = '\0';

    printf("\nFull name is %s.\n", name);
    return 0;
}

There is a simpler solution using string functions:

#include <stdio.h>
#include <string.h>

int main() {
    char fName[15], mName[15], lName[15], name[45];
    
    printf("\nEnter the First name: ");
    if (scanf("%14s", fName) != 1)
        return 1;
    printf("\nEnter the Middle name: ");
    if (scanf("%14s", mName) != 1)
        return 1;
    printf("\nEnter the Last name: ");
    if (scanf("%14s", lName) != 1)
        return 1;

    strcpy(name, fName);
    strcat(name, " ");
    strcat(name, mName);
    strcat(name, " ");
    strcat(name, lName);

    printf("\nFull name is %s.\n", name);
    return 0;
}

And even simpler using just snprintf:

#include <stdio.h>

int main() {
    char fName[15], mName[15], lName[15], name[45];
    
    printf("\nEnter the First name: ");
    if (scanf("%14s", fName) != 1)
        return 1;
    printf("\nEnter the Middle name: ");
    if (scanf("%14s", mName) != 1)
        return 1;
    printf("\nEnter the Last name: ");
    if (scanf("%14s", lName) != 1)
        return 1;

    snprintf(name, sizeof name, "%s %s %s", fName, mName, lName);

    printf("\nFull name is %s.\n", name);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

Others have explained why your program didn't print the full name. Phillip Ngan suggested a correction, but it pastes the individual names together without spacing. To get the full name in the usual form, you just need to replace the line i++; in your code with

            name[i++] = ' ';

and the line j++ with

            name[i+j++] = ' ';

Of course, you could replace all the for loops with just

    sprintf(name, "%s %s %s", fName, mName, lName);

And also of course you should note David C. Rankin's comment.

Armali
  • 18,255
  • 14
  • 57
  • 171