Shail
Shail

Reputation: 901

Combining two strings in C

I've just started learning about arrays and strings and I wanted to join the inputs of two strings, stringA and stringB and put their values in another string, stringC, after finding their lengths.

I want to solve it without built-in functions.

I've tried writing the code but I'm getting an error in the length of stringB and also the contents of stringC. Here is my code:

#include <stdio.h>
int main()
{
    char stringA[100];
    char stringB[100];
    char stringC[100];
    int lengthA=0;
    int lengthB=0;
    int i,j;

    printf("Enter a string A, terminate with a tilde.\n");
    scanf("%[^~]",stringA);
    while (stringA[lengthA] != '\0') 
        lengthA++;
    printf("The length is %d\n",lengthA+1);
    printf("Enter a string B, terminate with a !.\n");
    scanf("%[^!]",stringB);
    lengthB=0;
    while (stringB[lengthB] != '\0') 
        lengthB++;
    printf("The length is %d\n",lengthB+1);
    for(i=0;i<lengthA;i++)
        stringC[i]=stringA[i];
    for(i=lengthA;i<lengthA+lengthB;i++)
        stringC[i]=stringB[i];
    printf("%s",stringC);
    return 0;
}

Your help is appreciated!

Upvotes: 2

Views: 504

Answers (7)

SureshS
SureshS

Reputation: 609

The following question talks about input buffer which is not getting cleared in the above code. How to clear input buffer in C? Try using a debugger and check the contents of stringB after the second scanf statement.

Upvotes: 0

luke
luke

Reputation: 5

The index of stringB in the second loop is the problem. Remember that the index i is the lengthA at this point. Maybe you forgot to use your variable j ;-)

for(i=lengthA, j = 0; i < lengthA+lengthB; i++, j++)
    stringC[i] = stringB[j];

stringC[i + 1] = '\0';

As a hint: you dont need j:

for(i=lengthA; i < lengthA+lengthB; i++)
    stringC[i] = stringB[i - lengthA];

stringC[i + 1] = '\0';

Upvotes: 0

guillobuteler
guillobuteler

Reputation: 368

Jacob has point.

So, to fix your existing code:

char stringC[200];

And also, your indexes for stringB are wrong. This should work:

stringC[i]=stringB[i-lengthA];

Upvotes: 0

drewmm
drewmm

Reputation: 737

You're leaving the tilde and newline in after you import the first string. Since you're clearing a certain number of characters, you could just call getchar() a certain number of times, but there might be a more elegant way to do it.

In your loop where you're setting stringC, you're also indexing into stringB improperly.

Make sure you null terminate stringC, although I don't think it'll break this specific code. It's good practice.

Finally, you're outputting an extra +1 on the string lengths. That's not necessary (unless you're trying to count the ~ and ! as part of the length).

Upvotes: 0

Ganesh
Ganesh

Reputation: 5990

In this loop, you will have to eliminate the offset of lengthA for StringB as shown

for(i=lengthA;i<lengthA+lengthB;i++)
    stringC[i]=stringB[(i - lengthA)];

And add a NULL terminator after the loop as

stringC[i] = '\0';

Upvotes: 0

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

You ae counting from 0 in lengthA and you increment for every character until '\0', so lengthA is your actual length of string . You dont need lengthA+1

maybe appending a null character at the end of stringC will help?

Upvotes: 0

NPE
NPE

Reputation: 500615

Everything looks good up to this loop:

for(i=lengthA;i<lengthA+lengthB;i++)
    stringC[i]=stringB[i];

Here, you need to think about the index into stringB.

Also, don't forget about the NUL terminator.

edit: Also, don't forget that when you're reading stringB, the tilde and the newline are still on the input buffer.

Upvotes: 3

Related Questions