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

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

Aug 26, 2023 pm 12:17 PM
函數 c程式 字串傳遞

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
golang函數動態建立新函數的技巧 golang函數動態建立新函數的技巧 Apr 25, 2024 pm 02:39 PM

Go語言提供了兩種動態函數創建技術:closures和反射。 closures允許存取閉包作用域內的變量,而反射可使用FuncOf函數建立新函數。這些技術在自訂HTTP路由器、實現高度可自訂的系統和建置可插拔的元件方面非常有用。

C++ 函數命名中參數順序的考慮 C++ 函數命名中參數順序的考慮 Apr 24, 2024 pm 04:21 PM

在C++函數命名中,考慮參數順序至關重要,可提高可讀性、減少錯誤並促進重構。常見的參數順序約定包括:動作-物件、物件-動作、語意意義和遵循標準函式庫。最佳順序取決於函數目的、參數類型、潛在混淆和語言慣例。

excel函數公式大全 excel函數公式大全 May 07, 2024 pm 12:04 PM

1. SUM函數,用於對一列或一組單元格中的數字進行求和,例如:=SUM(A1:J10)。 2、AVERAGE函數,用於計算一列或一組儲存格中的數字的平均值,例如:=AVERAGE(A1:A10)。 3.COUNT函數,用於計算一列或一組單元格中的數字或文字的數量,例如:=COUNT(A1:A10)4、IF函數,用於根據指定的條件進行邏輯判斷,並返回相應的結果。

C++ 函式預設參數與可變參數的優缺點比較 C++ 函式預設參數與可變參數的優缺點比較 Apr 21, 2024 am 10:21 AM

C++函數中預設參數的優點包括簡化呼叫、增強可讀性、避免錯誤。缺點是限制靈活性、命名限制。可變參數的優點包括無限彈性、動態綁定。缺點包括複雜性更高、隱式型別轉換、除錯困難。

C++ 函式回傳參考型別有什麼好處? C++ 函式回傳參考型別有什麼好處? Apr 20, 2024 pm 09:12 PM

C++中的函數傳回參考類型的好處包括:效能提升:引用傳遞避免了物件複製,從而節省了記憶體和時間。直接修改:呼叫方可以直接修改傳回的參考對象,而無需重新賦值。程式碼簡潔:引用傳遞簡化了程式碼,無需額外的賦值操作。

如何在Java中寫出高效和可維護的函數? 如何在Java中寫出高效和可維護的函數? Apr 24, 2024 am 11:33 AM

編寫高效且可維護的Java函數的關鍵在於:保持簡潔。使用有意義的命名。處理特殊情況。使用適當的可見性。

自訂 PHP 函數和預定義函數之間有什麼區別? 自訂 PHP 函數和預定義函數之間有什麼區別? Apr 22, 2024 pm 02:21 PM

自訂PHP函數與預定義函數的差異在於:作用域:自訂函數僅限於其定義範圍,而預定義函數可在整個腳本中存取。定義方式:自訂函數使用function關鍵字定義,而預先定義函數則由PHP核心定義。參數傳遞:自訂函數接收參數,而預先定義函數可能不需要參數。擴充性:自訂函數可以根據需要創建,而預定義函數是內建的且無法修改。

C++ 函式異常進階:客製化錯誤處理 C++ 函式異常進階:客製化錯誤處理 May 01, 2024 pm 06:39 PM

C++中的異常處理可透過自訂異常類別增強,提供特定錯誤訊息、上下文資訊以及根據錯誤類型執行自訂操作。定義繼承自std::exception的異常類,提供特定的錯誤訊息。使用throw關鍵字拋出自訂異常。在try-catch區塊中使用dynamic_cast將捕獲到的異常轉換為自訂異常類型。在實戰案例中,open_file函數會拋出FileNotFoundException異常,捕捉並處理該異常可提供更具體的錯誤訊息。

See all articles