简体   繁体   English

c中char数组中的空格

[英]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 : 在两个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 : 在第一个scanf中输入“ tom”值并在第二个scanf中输入“ fox”值时的输出:

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. 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] [编辑2]

OP's latest does not terminate the string. OP的最新版本不会终止字符串。

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

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

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++){

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). 函数scanf()不返回读入的字符数。而是返回其读入(或错误)的成功匹配数。 In your runs, scanf() is returning 1, so you only copy the first character from first and last name to full name. 在您的运行中,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初始化为全零或nul将其终止。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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