首页  >  文章  >  后端开发  >  C++程序将字符串传递给函数

C++程序将字符串传递给函数

PHPz
PHPz转载
2023-08-26 12:17:121232浏览

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 datatypes. The syntax is also quite similar.

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 whether the string is palindrome or not. Let 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() by 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 or a character pointer to the base address 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 array, not the character pointer. And the isPalindrome() will 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 entering into the code.

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删除