首頁 >後端開發 >C++ >C++程式將字串傳遞給函數

C++程式將字串傳遞給函數

PHPz
PHPz轉載
2023-08-26 12:17:121248瀏覽

C++程式將字串傳遞給函數

任何使用函數的程式語言都具有更簡單、更模組化且在偵錯時更容易更改的程式碼。函數是模組化程式碼中非常有益的組成部分。函數可以接受參數並對其執行某些操作。與其他原始資料類型一樣,我們也可以將物件類型或陣列作為參數傳遞。在本文中,我們將看到如何在C 中將字串類型的資料作為函數參數傳遞。

傳遞類似C 字串的參數給函數

C supports stronger string objects which is actually a class with different member functions associated with them. A string object passing as an argument is similar to the passing of normal primitive normals.

Syntax

<return type> function_name ( string argument1, string argument2, … ) {
   // function body
}

In the following example, we will see a program to check whether a given string is a palindrome or not. There will be two functions, one will reverse the string, and another will check notether the string 是 whe pal. us see the algorithm and corresponding C implementation.

演算法

    define a function reverse(), this will take a string s
  • n := floor of (length of s / 2)
  • for i ranging from 0 to n/2; do
    • temp := s[i]
    • s[i] := s[ n - i - 1 ]
    • s[ n - i - 1 ] := temp
end for
  • return s
  • end of reverse() function
  • ##定義一個函數 isPalindrome(),它將接受參數 s
  • revS := call reverse() 由 passing s to reverse the string s
  • 如果 s 和 revS 相同,則
    • return True
  • otherwise
    • return False
  • end if
  • isPalindrome()函數結束
  • Example

    的中文翻譯為:

    範例

    #include <iostream>
    #include <sstream>
    
    using namespace std;
    string reverse( string s ) {
       char temp;
       int n = s.length();
       for( int i = 0; i < n / 2; i++ ) {
          temp = s[i];
          s[i] = s[ n - i - 1 ];
          s[ n - i - 1 ] = temp;
       }
       return s;
    }
    
    string isPalindrome( string s ) {
       string revS = reverse( s );
       if( s == revS ) {
          return "True";
       }
       else {
          return "False";
       }
    }
    
    int main()
    {
       cout << "Is "racecar" a palindrome? " << isPalindrome( "racecar" ) << endl;
       cout << "Is "abcdef" a palindrome? " << isPalindrome( "abcdef" ) << endl;
       cout << "Is "madam" a palindrome? " << isPalindrome( "madam" ) << endl;
       cout << "Is "sir" a palindrome? " << isPalindrome( "sir" ) << endl;
    }
    

    Output

    #
    Is "racecar" a palindrome? True
    Is "abcdef" a palindrome? False
    Is "madam" a palindrome? True
    Is "sir" a palindrome? False
    

    Passing C-like character array to a function

    #Since C supports almost all that is supported by C, we can also define strings using a character array like C. To pass C-like strings to a function, it must pass a character array ordacter array ordacter baseer baseer baseer of the string. The syntaxes are like below −

    #Syntax

    (使用字元指標)

    <return type> function_name ( char* <string variable>, … ) {
       // function body
    }
    

    Syntax

    (使用字元陣列)

    <return type> function_name ( char <string variable>[], … ) {
       // function body
    }
    

    Let us see the same example of palindrome checking with character array passing. Here the reverse() function will modify the array, so we must pass this string as a character 腳just check whether the string is the same as the reversed string, so it can take character pointer or character array, and the effect will be the same. The algorithm is similar so we are directly ing in#to

    Example

    的中文翻譯為:

    範例

    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    
    void reverse( char s[] ) {
       char temp;
       int n = strlen( s );
       for( int i = 0; i < n / 2; i++ ) {
          temp = s[i];
          s[i] = s[ n - i - 1 ];
          s[ n - i - 1 ] = temp;
       }
    }
    
    string isPalindrome( char* s ) {
       char* sRev = (char*) malloc( strlen(s) );
       strcpy( sRev, s );
       reverse( sRev );
       if( strcmp( sRev, s ) == 0 ) {
          return "True";
       }
       else {
          return "False";
       }
    }
    
    int main()
    {
       string s = "racecar";
       cout << "Is "racecar" a palindrome? " << isPalindrome( const_cast<char*> (s.c_str()) ) << endl; 
       s = "abcdef";
    
       cout << "Is "abcdef" a palindrome? " << isPalindrome( const_cast<char*> (s.c_str()) ) << endl; 
       s = "madam";
    
       cout << "Is "madam" a palindrome? " << isPalindrome( const_cast<char*> (s.c_str()) ) << endl; 
       s = "sir";
    
       cout << "Is "sir" a palindrome? " << isPalindrome( const_cast<char*> (s.c_str()) ) << endl;
    }
    

    Output

    #
    Is "racecar" a palindrome? True
    Is "abcdef" a palindrome? False
    Is "madam" a palindrome? True
    Is "sir" a palindrome? False
    

    在這個範例中,我們看到在C 中調整C樣式字串有幾個步驟。對於C樣式字串,使用cstring庫來取得長度、字串比較和其他操作。從C 字串到C字串的轉換,需要使用c_str()函數,但是這個函數傳回const char*,然而我們的函數只接受char*類型的資料。對於這種情況,我們需要使用const_cast將值轉換為char*類型。

    Conclusion

    函數可以接受原始資料型別以及陣列、物件類型等。當我們使用字串時,在C 中它們是物件類型,而在C中是字元陣列類型。但由於C 也支援C語法,所以在C 中也是有效的。傳遞一個字串物件很簡單,但是傳遞一個字元陣列需要特別注意並遵循一些嚴格的步驟。 C風格的字串可以以陣列格式或字元指標的形式傳遞。當我們知道函數會改變字串本身時,我們必須將字串作為字元陣列傳遞,否則,從指標修改字串是不允許的。當字串只被使用時,我們可以使用指標或字元陣列進行傳遞,效果是相同的。但在這種情況下,透過字元數組傳遞是好的,因為它會阻止對字串的無意更新。

    以上是C++程式將字串傳遞給函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除