0

0

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

PHPz

PHPz

发布时间:2023-08-26 12:17:12

|

1733人浏览过

|

来源于tutorialspoint

转载

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

 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 
#include 

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 −

立即学习C++免费学习笔记(深入)”;

Syntax

(使用字符指针)

居然设计家
居然设计家

居然之家和阿里巴巴共同打造的家居家装AI设计平台

下载
 function_name ( char* , … ) {
   // function body
}

Syntax

(使用字符数组)

 function_name ( char [], … ) {
   // 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 
#include 
#include 
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 (s.c_str()) ) << endl; 
   s = "abcdef";

   cout << "Is "abcdef" a palindrome? " << isPalindrome( const_cast (s.c_str()) ) << endl; 
   s = "madam";

   cout << "Is "madam" a palindrome? " << isPalindrome( const_cast (s.c_str()) ) << endl; 
   s = "sir";

   cout << "Is "sir" a palindrome? " << isPalindrome( const_cast (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++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

本专题整合了AppleID相关内容,阅读专题下面的文章了解更多详细教程。

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 5.9万人学习

Django 教程
Django 教程

共28课时 | 2.4万人学习

Excel 教程
Excel 教程

共162课时 | 9.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号