2016-03-18 34 views
-1

我不得不连接两个按字母顺序排序的ntcas(以空字符结尾的字符数组)。我首先尝试使用像strncat,strcat这样的函数来做到这一点,但我无法让程序正常工作。如何使用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(); 
} 
+4

** TL; DR **只需使用'的std :: string'代替'炭ntca1 [251],ntca2 [251 ],结果[255];'。它会让你的生活变得更轻松。你的散文描述的问题几乎是不可读的,结构严重的BTW。 –

+0

我不知道在这个时候使用它,我应该解决我提到的功能 – Skyp89

+0

@ Skyp89:为什么“应该”你解决你提到的功能?只需使用'std :: string'。 –

回答

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

所以程序从'ntca1'或'ntca2'中复制剩余的字符到'result'中,但'strncat'中的'sizeof(result)'是什么意思?不应该是关于我们连接的源的大小('ntca1'或'ntca2')或我们连接到'result'的多少个字符? – Skyp89

+1

'strncat'会从'ntcaX'移动到'sizeof(result)'(501)个字符,在'i'或'j'处指向终止的NULL到'result'。这实际上是我的一个错误。我们希望最多移动'sizeof(result) - 1 - k'个字节来防止溢出。将修复答案。 – user4581301

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)); 
相关问题