簡體   English   中英

在c ++中添加兩個c字符串

[英]adding two c-strings in c++

在我的代碼中,我試圖將兩個字符串添加到一起,但由於某種原因,我似乎無法獲得我的stringAdd函數的正確返回類型。 我希望能夠返回一個c字符串。 我的實現似乎也沒有用。 有什么建議么?

 #include <iostream>
 #include<cstring>

 using namespace std;
 int stringLength(char *); // Function prototype 
 char stringAdd(char *strPtr, char *strPtr2);//Function prototype

int main()
{
   const int SIZE = 51; // Array size 
   char letter; // The character to count 
   char word1[SIZE] = "Happy ";
   char word2[SIZE] = "Birthday";

   cout <<"Your first c-string is: "<<word1<<"Your second c-string is: "<<word2<<"\n";
   cout << "The length of your first c-string is: ";

   cout << stringLength(word1) << " chars long.\n";
   cout << "The length of your second c-string is: ";

   cout << stringLength(word2) << " chars long.\n";

   if (SIZE >= (stringLength(word1) + stringLength(word2) + 1))
   {
      cout << "we are gunna add ur strings";
      stringAdd(word1, word2);
   }
   else
   {
      cout << "String1 is not large enough for both strings.\n";
   }
   return 0;
}

int stringLength(char *strPtr)
{
    int times = 0; // Number of times a char appears in the string 

    // Step through the string each char. 
    while (*strPtr != '\0')
    {
        if (*strPtr != '0') // If the current character doesnt equals the null terminator... 
           times++; // Increments the counter 
        strPtr++; // Goes to the next char in the string. 
    }
    return times;
}

到目前為止,我的代碼工作正常但是下面的函數似乎根本不起作用。 我不確定如何使用引用添加兩個c字符串

char stringAdd(char *strPtr, char *strPtr2)
{
   int size1;
   int size2;
   size1= stringLength(strPtr);
   int j=size1+1; // counter set to the num of chars in the first c-string
   int i = 0; // counter for to add to the 2nd c-string
   size2= stringLength(strPtr2);
   size1=+size2;
   char newWord[size1];

   for(int i=0;i<size1;i++)
      newWord[i] = *strPtr[i]
   for(int j=0;j<size2;j++)
      newWord[i]= *str
}

首先,使用std::string

然后,使用std::string

最后,如果你真的必須手動操作char數組,那么至少使用C標准庫函數,這樣你就有希望得到正確的null終止。 你正在尋找的函數是std::strcat ,它連接兩個字符串。

之后,使用std::string

你在stringAddstringAdd了一個錯誤導致錯誤

size1=+size2;

這應該是

size1 += size2;

否則,你只是簡單地覆蓋size1與價值size2 話雖如此,在C ++中你也不允許這樣做

char newWord[size1];

必須在編譯時而不是運行時知道數組的大小。

函數stringAdd不正確,並且具有未定義的行為,因為它不返回任何內容。

也是函數stringLength if語句

if (*strPtr != '0') // If the current character doesnt equals the null terminator... 

    times++; // Increments the counter 

doea沒有多大意義,因為封閉while語句中的條件

while (*strPtr != '\0')

與if語句中的相同。

{

可以通過以下方式編寫函數

size_t stringLength( const char *strPtr )
{
    size_t n = 0;

    while ( strPtr[n] ) ++n;

    return n;
}


char * stringAdd( char *strPtr, const char *strPtr2 )
{
    char *p = strPtr + stringLength( strPtr );

    while ( *p++ = *strPtr2++ );

    return strPtr;
}

在主要你可以寫

if (SIZE >= (stringLength(word1) + stringLength(word2) + 1)) {
    cout << "we are gunna add ur strings" << endl;
    cout << stringAdd(word1, word2) << endl;
}
//...

在這種情況下,word2將附加到word1。

你把它標記為“c ++”和“c-strings”,這有點像要求一輛由腳踏力驅動的汽車。

你有2個選擇:

  1. 使用C ++字符串
  2. 使用C字符串

對於前者:

#include <iostream>
#include <string>

int main()
{
    std::string word1 = "Happy";
    std::string word2 = "Birthday";
    // ... your other stuff
    std::string result = word1 + " " + word2 + "!";
    std::cout << "Result is " << result << std::endl;
    return 0;
}

對於后者:

#include <iostream> // if you are stuck using c-strings, this is kind of odd
#include <cstring>
#include <memory>

int main()
{
    const char* word1 = "Happy";
    const char* word2 = "Birthday";
    const unsigned int newWordSize = 20; // you only need 16 for this, so 20 is sufficient
    char newWord[newWordSize];
    std::memset(newWord, newWordSize, 0);
    std::strcpy(newWord, word1);
    std::strcat(newWord, " ");
    std::strcat(newWord, word2);
    std::strcat(newWord, "!");
    std::cout << "New Word is " << newWord << std::endl;
    return 0;
}

為什么你在做什么是錯的:

// NOTE:  If your null-terminators are not set, this breaks as it is an infinite loop.
int stringLength(char *strPtr)
{
    // NOTE:  pointer arithmetic can speed up this function, and make this variable unnecessary
    int times = 0; // Number of times a char appears in the string 

    // Step through the string each char. 
    while (*strPtr != '\0')
    {
        if (*strPtr != '0') // If the current character doesnt equals the null terminator... 
           times++; // Increments the counter 
        strPtr++; // Goes to the next char in the string. 
    }
    return times;
}

char stringAdd(char *strPtr, char *strPtr2) // ERROR:  your return value should probably be char*, and you will need to free that memory later
{
   int size1;
   int size2;
   size1= stringLength(strPtr);
   int j=size1+1; // counter set to the num of chars in the first c-string
   int i = 0; // counter for to add to the 2nd c-string
   size2= stringLength(strPtr2);
   size1=+size2;
   char newWord[size1]; // ERROR:  you cannot allocate a dynamic array this way.

   for(int i=0;i<size1;i++) // ERROR:  you've set size1 = size1 + size2 + 1, and you attempt to access the first word with this new size.  You will access memory outside the bounds of your array
      newWord[i] = *strPtr[i]
   for(int j=0;j<size2;j++)
      newWord[i]= *str
    // ERROR:  You do not set the null-terminator for the new string
    // ERROR:  you do not return anything
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM