简体   繁体   中英

spaces in char array in c

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

The return value from scanf() is the number of fields successfully scanned, not the length of some string.

Use:

if (1 != scanf("%14s",first_name)) Handle_EOF_orNoInput();
first_name_length = strlen(first_name);`

Same for last_name


[Edit]

Since OP wants to not introduce new functions (like even strlen() ), use "%n to find the length. "%n record the parsing position.

if (1 != scanf("%14s%n",first_name, &first_name_length)) Handle_EOF_orNoInput();

OR find length the hard way

if (1 != scanf("%14s",first_name)) Handle_EOF_orNoInput();
for (first_name_length = 0; first_name[first_name_length]; first_name_length++);

[Edit 2]

OP's latest does not terminate the string.

full_name[i] = '\0';  // add this
printf("%s",full_name);

Rewritten without the sprintf function and without pointers

#include <stdio.h>
int main()
{
    char first_name[15], last_name[15] ,full_name[32];
    int src, dest;
    printf("enter your first name\n");
    scanf("%s",first_name);
    printf("enter your last name\n");
    scanf("%s",last_name);

    dest = 0;
    src = 0;
    while (first_name[src] != 0) full_name[dest++] = first_name[src++];
    full_name[dest++] = ' ';
    src = 0;
    while (last_name[src] != 0) full_name[dest++] = last_name[src++];
    full_name[dest] = 0
    printf("%s",full_name);
    return 0;
}

Change

i++;
full_name[i]=' ';

to

full_name[i++]=' ';

Your for loop conditions are also not quite right:

for(i2 = 0;i2 < first_name_length;i2++){

The function scanf() does not return the number of characters read in. Rather, it returns the number of successful matches it read in (or error). In your runs, scanf() is returning 1, so you only copy the first character from first and last name to full name.

Also, you need to initialize full_name to all zeroes or nul terminate it after you are done copying the last name.

Furthermore, the way you are reading in the strings is dangerous. You should restrict how many characters the scanf() can read in, otherwise it will overflow your buffers and trash your memory (or let a hacker in, etc.):

if (1 != scanf("%14s", first_name))
  ; // TODO: handle unexpected input, EOF, error, etc.

Or even better:

#define STR(x) #x
#define SSTR(x) STR(x)
#define STR_FMT(x) "%" SSTR(x) "s"
#define FIRST_NAME_MAX_LEN 14

...

char first_name[FIRST_NAME_MAX_LEN + 1] = { 0 };

...

if (1 != scanf(STR_FMT(FIRST_NAME_MAX_LEN), first_name))
  ;  // TODO: handle unexpected input, EOF, error, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM