Subhadeep Banerjee
Subhadeep Banerjee

Reputation: 3

How to add whitespace in between two string in C

I am making strcat version of my own in C, but I am not able to add whitespace in between the concatenated words. ps: Searched a lot got no appropriate answer.

#include <stdio.h>
#include <conio.h>
int xstrlen(char *a)
{
    int count = 0;
    while (*a != '\0')
    {
        count++;
        a++;
    }
    return count;
}
void xstrcpy(char *a, char *b, char *c)
{
    while (*b != '\0')
    {
        *a++ = *b;
        *b++;
    }
    while (*c != '\0')
    {
        *a++ = *c;
        *c++;
    }
    *a = '\0';
}
void xstrcat(char *a, char *b)
{
    xstrcpy(a, a, b);
}
int main()
{
    char src[20], tar[20];
    puts("First Name");
    gets(src);
    puts("Last Name");
    gets(tar);
    printf("Executing...\n");
    xstrcat(src, tar);
    printf("Full Name - %s", src);

    printf("\nEnter any Key to exit...");
    getch();
    return 0;
}

tried adding *a = ' ' between two while loops in xstrcpy, but it did not work.

Upvotes: 0

Views: 211

Answers (1)

Mohit Hurkat
Mohit Hurkat

Reputation: 416

It could be as easy as

        *a++=' ';

Complete Code:

void xstrcpy(char *a, char *b, char *c)
{
    while (*b != '\0')
    {
        *a++ = *b++;
    }

    *a++=' ';

    while (*c != '\0')
    {
        *a++ = *c++;
    }
    *a = '\0';
}

Upvotes: 2

Related Questions