0

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
6
  • 1
    Your first_name and last_name are 15 char each and full_name is 30. If you add a space between a max length first_name and max length last_name, you're going to be in trouble. Commented Feb 26, 2015 at 19:08
  • i edited my question what's now it doesn't work too it output only the first name but the last name not shown Commented Feb 26, 2015 at 19:10
  • You're not incrementing i after you set the space, you're incrementing it before hand. Commented Feb 26, 2015 at 19:11
  • i edited my question but it still output only the first name but the last name not shown Commented Feb 26, 2015 at 19:12
  • @tom fox Not good to update your post in the fashion you did. Recommend reverting to the previous version and appending new code, data or insights. Commented Feb 26, 2015 at 19:48

4 Answers 4

4

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);
Sign up to request clarification or add additional context in comments.

16 Comments

sorry i can't use any other functions expect the written in the code
@tomfox This is a great answer, you can iterate through first_name and look for '/0' instead of strlen.
@Dima Maligin Just put that in.
i updated the code to simple way but the problem is in the unknown chars
The problem with your update is that you did not terminate the string. add full_name[i] = 0; before print.
|
1

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

2 Comments

sorry i can't use any other functions expect the written in the code
i can't use the * before the variables
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++){

5 Comments

it out put some unknown characters in the end of the name
it loops until the chars of the array are finished what's the problem here i didn't type first_name_length any where in the code
Can you update with your code changes and the output you're getting?
Looks like I should have checked a better source for the return value of scanf().
i updated the code to simple way but the problem is in the unknown chars
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.

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.

2 Comments

for some reason i can't use define
@tomfox Umm, ok, then just hardcode the constants even though that's typically bad form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.