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 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 sn := floor of (length of s / 2)for i ranging from 0 to n/2; dotemp := s[i]s[i] := s[ n – i – 1 ]s[ n – i – 1 ] := tempend forreturn send of reverse() function定义一个函数 isPalindrome(),它将接受参数 srevS := call reverse() by passing s to reverse the string s如果 s 和 revS 相同,则return Trueotherwisereturn Falseend ifisPalindrome()函数结束

Example

的中文翻译为:

示例

#include #include using namespace std;string reverse( string s ) {   char temp;   int n = s.length();   for( int i = 0; i 

Output

Is "racecar" a palindrome? TrueIs "abcdef" a palindrome? FalseIs "madam" a palindrome? TrueIs "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

(使用字符指针)

 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  (s.c_str()) )  (s.c_str()) )  (s.c_str()) )  (s.c_str()) ) 

Output

Is "racecar" a palindrome? TrueIs "abcdef" a palindrome? FalseIs "madam" a palindrome? TrueIs "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++程序将字符串传递给函数的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2587196.html

(0)
上一篇 2025年3月6日 15:31:03
下一篇 2025年2月26日 17:35:10

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论