c中char数组中的空格

时间:2022-04-14 21:42:21

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 :

输入两个scanf中的“name”值时的输出:

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 :

输入第一个scanf中的“tom”值和第二个scanf中的“fox”值时的输出:

tom fox↓@

and it should be :

它应该是:

tom fox

4 个解决方案

#1


4  

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

scanf()的返回值是成功扫描的字段数,而不是某些字符串的长度。

Use:

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

Same for last_name

对于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.

由于OP想要不引入新函数(比如strlen()),使用“%n来查找长度。”%n记录解析位置。

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.

OP的最新版本不会终止该字符串。

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

#2


1  

Rewritten without the sprintf function and without pointers

没有sprintf函数而没有指针重写

#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;
}

#3


0  

Change

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

to

full_name[i++]=' ';

Your for loop conditions are also not quite right:

你的for循环条件也不太对劲:

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

#4


0  

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.

函数scanf()不返回读入的字符数。而是返回它读入的成功匹配数(或错误)。在您的运行中,scanf()返回1,因此您只将第一个字符从名字和姓氏复制到全名。

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

此外,您需要将full_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.):

此外,你阅读字符串的方式是危险的。你应该限制scanf()可以读入多少个字符,否则它会溢出你的缓冲区并丢弃你的记忆(或让黑客进入等):

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.

#1


4  

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

scanf()的返回值是成功扫描的字段数,而不是某些字符串的长度。

Use:

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

Same for last_name

对于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.

由于OP想要不引入新函数(比如strlen()),使用“%n来查找长度。”%n记录解析位置。

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.

OP的最新版本不会终止该字符串。

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

#2


1  

Rewritten without the sprintf function and without pointers

没有sprintf函数而没有指针重写

#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;
}

#3


0  

Change

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

to

full_name[i++]=' ';

Your for loop conditions are also not quite right:

你的for循环条件也不太对劲:

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

#4


0  

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.

函数scanf()不返回读入的字符数。而是返回它读入的成功匹配数(或错误)。在您的运行中,scanf()返回1,因此您只将第一个字符从名字和姓氏复制到全名。

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

此外,您需要将full_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.):

此外,你阅读字符串的方式是危险的。你应该限制scanf()可以读入多少个字符,否则它会溢出你的缓冲区并丢弃你的记忆(或让黑客进入等):

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.