-1

I had to concatenate two ntcas (null terminated character arrays) that are alphabetically sorted. I tried first to do this using functions like strncat, strcat, but I just can't make the program to work properly.

Let's say that ntca1 is "ace" and ntca2 is "bdfghz". I compare the first character from the two ntcas. I concatenate the character that had a smaller value to the result ntca. I increment the index of the ntca from which the character was copied by 1.

This continues until one of the strings ends. Then the program copies any remaining from the other string to result.

But ... I realised that I didn't know how to concatenate the character from a specific index of the source to the end of the destination ntca. Should I use the strcat or strncat?

In the second part of the problem, when one of the ntcas is over, how can I concatenate the remaining characters to result?

This is what I have so far

#include <iostream>
#include <string.h>

using namespace std;

int main()

{   char ntca1[251], ntca2[251], result[255];
    unsigned int i, j, k, lenght_ntca1, lenght_ntca2;

    cout << "Type the first ntca and press ENTER:";
    cin.getline(ntca1, 251);
    cout << "Type the second ntca and press ENTER:";
    cin.getline(ntca2, 251);
    cout << endl;

    i = 0;
    j = 0;
    k = 0;

    lenght_ntca1 = strlen(ntca1);
    lenght_ntca2 = strlen(ntca2);

    while (i<lenght_ntca1 && j<lenght_ntca2)
    {   if (ntca1[i] <= ntca2[j])
        {   result[k]=ntca1[i];
            i++;    
        }
        else if (ntca1[i] > ntca2[j])
        {   result[k] = ntca2[j];
            j++;    
        }
    k++;
    }

    if (i == lenght_ntca1)
        while (j<=lenght_ntca2)
        {   result[k] = ntca2[j];
            k++;
            j++;
        }
    else if (j == lenght_ntca2)
        while (i <= lenght_ntca1)
        {   result[k] = ntca1[i];
            k++;
            i++;
        }

    cout << "The third ntca is: ";
    cout << result;

    cin.get();
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
CCBet
  • 416
  • 1
  • 6
  • 19
  • 4
    **TL;DR** Simply use `std::string` instead of `char ntca1[251], ntca2[251], result[255];`. It will make your life a lot easier. Your prose description of the problem is nearly unreadable and badly structured BTW. – πάντα ῥεῖ Mar 18 '16 at 17:08
  • I don't know to use this at this moment and I should solve the functions that i mentioned – CCBet Mar 18 '16 at 17:11
  • @Skyp89: Why "should" you solve the functions that you mentioned? Just use `std::string`. – Christian Hackl Mar 18 '16 at 18:15
  • you mean why should I "use" ... Because this problem is from a document about these specific functions ... so it should be solvable using these tools. – CCBet Mar 18 '16 at 18:23
  • @Skyp89. I have tried to reduce your problem description without damaging the intent. Please read it over and check that all I didn't remove anything important. – user4581301 Mar 18 '16 at 18:24
  • 1
    off topic recommendations: include `` rather than ``. Be very wary of `using namepace std;`. I think you are getting away with it here, but it can cause some really neat mystery bugs. Read more here: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice – user4581301 Mar 18 '16 at 18:27
  • Let's say that it's ok, but the code that I attached has nothing to do with the strcat or strncat functions (because you wrote that the code is "what I have so far"). It's just how i managed to solve the problem using other tools. – CCBet Mar 18 '16 at 18:29
  • Gotcha. To be honest, because you are moving one character at a time, `strcat` is useless up until the end where you move the remainder of one of the strings. Your first while loop is better-performing and simpler than any result you are going to get from `strcat`. – user4581301 Mar 18 '16 at 18:32
  • if you're saying that it's useless until the part where I should copy the rest of the characters, how could I use the strcat in this situation ? – CCBet Mar 18 '16 at 18:35

2 Answers2

1
#include <iostream>
#include <cstring> // C++ include

//removed using namespace std

int main()
{
    char ntca1[251], ntca2[251], result[501];
    // probably want a bigger result. 250 chars + 250 chars = 500 chars. 
    // plus 1 for the null. result can now take anything the ntcas can give

    unsigned int i, j, k, lenght_ntca1, lenght_ntca2;

    std::cout << "Type the first ntca and press ENTER:";
    std::cin.getline(ntca1, 251);
    std::cout << "Type the second ntca and press ENTER:";
    std::cin.getline(ntca2, 251);
    std::cout << std::endl;

    i = 0;
    j = 0;
    k = 0;

    lenght_ntca1 = strlen(ntca1);
    lenght_ntca2 = strlen(ntca2);

You can't really do much better than this loop. I just added a test to prevent overflowing result and tweaked the else if logic. If A is not greater than or equal to B it must be less than. No point testing.

    while (i < lenght_ntca1 &&
           j < lenght_ntca2 &&
           k < sizeof(result) - 1) // test for result overflow. Just in case
    {
        if (ntca1[i] <= ntca2[j])
        {
            result[k] = ntca1[i];
            i++;
        }
        else // if gone
        {
            result[k] = ntca2[j];
            j++;
        }
        k++;
    }

Here is where strcat becomes useful:

    result [k] = '\0'; //null terminate k

    //copy over the remains of either ntca
    if (i < lenght_ntca1)
    {
        strncat(result, &ntca1[i], sizeof(result) - 1 - k);
    }
    else if (j < lenght_ntca2)
    {
        strncat(result, &ntca2[j], sizeof(result) - 1 - k);
    }
    std::cout << "The third ntca is: ";
    std::cout << result;

    std::cin.get();
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • so the program copies the rest of characters from `ntca1` or `ntca2`, in `result`, but what would mean in the `strncat` the `sizeof(result)` ? shouldn't it be something about the size of the source from which we concatenate (`ntca1` or `ntca2`)or how many characters we concatenate to the `result`? – CCBet Mar 18 '16 at 19:11
  • 1
    `strncat` will move up to `sizeof(result)` (501) characters from `ntcaX` at stating at `i` or `j` to the terminating NULL into `result`. This is actually a bug on my part. We want to move at most `sizeof(result) - 1 - k` bytes to prevent overflow. Will fix answer. – user4581301 Mar 18 '16 at 19:44
0
std::string res;
std::string first = "ace";
std::string second = "bdfghz";
std::merge(first.begin(), first.end(),
    second.begin(), second.end(),
    std::back_inserter(res));
Pete Becker
  • 74,985
  • 8
  • 76
  • 165