如何使用strncat,strcat等函数获得相同的结果?

时间:2016-03-18 17:06:28

标签: c++ string

我不得不连接两个按字母顺序排序的ntcas(空终止字符数组)。我首先尝试使用strncat,strcat等函数来完成此操作,但我不能让程序正常工作。

让我们说ntca1"ace"ntca2"bdfghz"。我比较了两个ntcas的第一个角色。我将具有较小值的字符连接到result ntca。我将字符复制的ntca的索引增加1.

这一直持续到其中一个字符串结束。然后程序将剩余的其他字符串复制到result

但是......我意识到我不知道如何将字符从源的特定索引连接到目标ntca的末尾。我应该使用strcat还是strncat?

在问题的第二部分,当其中一个ntcas结束时,如何将剩余的字符连接到result

这是我到目前为止所拥有的

#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();
}

2 个答案:

答案 0 :(得分: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);

你不能比这个循环做得更好。我刚刚添加了一个测试来防止溢出结果并调整其他逻辑。如果A不大于或等于B,则必须小于。没有点测试。

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

以下是strcat变得有用的地方:

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

答案 1 :(得分: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));