C / C ++ char指针问题 [英] C/C++ char pointer question

查看:74
本文介绍了C / C ++ char指针问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于以下C / C ++问题的帮助。

这是相当直接但我有一些

的困难。

感谢您的帮助。


您将获得以下功能签名:


void addParen(char * str)


编写函数体的代码,用开括号括起来封闭str的每个

字符。对于

示例:Hello!将改为(H)(e)(l)(l)(o)(!)。

I need some help with the following C/C++ question.
It is fairly straight forward but I''m having some
difficulties.
Thanks for your help.

You are given the following function signature:

void addParen(char* str)

Write code for the function body that will enclose every
character of str with open and close parenthesis. For
example: "Hello!" will change to "(H)(e)(l)(l)(o)(!)".

推荐答案



t-tocs写道:

t-tocs wrote:
我需要一些关于以下C / C ++问题的帮助。
这是相当直接但我有一些
困难。
感谢您的帮助。

您将获得以下功能签名:

void addParen(char * str)


有一个很好的理由说明为什么不能


void addParen(const std :: string& str)

>
char数组是表示字符串的低级别hack。经常

std :: string优越。

编写函数体的代码,用开括号括起来封闭str的每个
字符。对于
示例:Hello!将改为(H)(e)(l)(l)(o)(!)。
I need some help with the following C/C++ question.
It is fairly straight forward but I''m having some
difficulties.
Thanks for your help.

You are given the following function signature:

void addParen(char* str)
Is there a good reason why that can''t be

void addParen(const std::string& str)

arrays of char are a low level hack to represent strings. Often
std::string is superior.
Write code for the function body that will enclose every
character of str with open and close parenthesis. For
example: "Hello!" will change to "(H)(e)(l)(l)(o)(!)".




你有什么困难?发布您的代码,遵循

指南

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


你会得到很多帮助。


你的问题表达方式听起来很像家庭作业。

如果这是真的,请记住,你在这里找不到很多人

准备好为你做你的工作,但是如果<你将获得所需的帮助br />
你展示你做了什么,并解释你遇到什么问题

with。


Gavin Deane



What difficulties are you having? Post your code, following the
guidelines here

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

and you will get plenty of help.

The way you''ve worded your question makes it sound a lot like homework.
If that''s true, bear in mind that you won''t find many people here
prepared to do your work for you, but you will get the help you need if
you show what you''ve done and explain what it is you are having trouble
with.

Gavin Deane


Gavin Deane写道:
Gavin Deane wrote:
t-tocs写道:
我需要一些以下C的帮助/ C ++问题。
这是相当直接但我有一些困难。
感谢您的帮助。

你是一个重新给出以下函数签名:

void addParen(char * str)
I need some help with the following C/C++ question.
It is fairly straight forward but I''m having some
difficulties.
Thanks for your help.

You are given the following function signature:

void addParen(char* str)



有充分的理由说明为什么不能这样做

void addParen(const std :: string& str)



Is there a good reason why that can''t be

void addParen(const std::string& str)




我想是的:P


试试:


std :: string addParen(const std :: string& str)



void addParen(std :: string& str)


:)


Ben Pope

-

我不只是一个号码。对很多人来说,我被称为字符串......



I think so :P

try:

std::string addParen(const std::string& str)
or
void addParen(std::string& str)

:)

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


文章< 11 **************** ******@g43g2000cwa.googlegroups .com> ;,

" t-tocs" < SC ******** @ gmail.com>写道:
In article <11**********************@g43g2000cwa.googlegroups .com>,
"t-tocs" <sc********@gmail.com> wrote:
我需要一些关于以下C / C ++问题的帮助。
这是相当直接的,但我有一些困难。 />感谢您的帮助。

您将获得以下功能签名:

void addParen(char * str)

编写代码函数体将用开括号括起来包围str的每个
字符。对于
示例:Hello!将改为(H)(e)(l)(l)(o)(!)。
I need some help with the following C/C++ question.
It is fairly straight forward but I''m having some
difficulties.
Thanks for your help.

You are given the following function signature:

void addParen(char* str)

Write code for the function body that will enclose every
character of str with open and close parenthesis. For
example: "Hello!" will change to "(H)(e)(l)(l)(o)(!)".




我会先抱怨。是什么内存分配给str

传入?如果它无法处理我需要的所有额外字符

加入它怎么办?这是一个非常危险的功能签名。


说,你需要设置一些测试:


#include< algorithm>

#include< iostream>

#include< string>

#include< vector>


使用命名空间std;


void addParen(char * str)

{

//将你的代码放在这里

}


void test_addParen(const string& input,const string& expected){

std :: vector< char> str(input.size()* 3 + 1);

copy(input.begin(),input.end(),str.begin());


addParen(& str [0]);


if(expected!=& str [0]){

cout< < 输入测试失败" <<输入

<< "'':expected''" <<预期

<< 但是得到了'' << & str [0]<< "''\ n";

抛-1;

}

}


int main()

{

test_addParen(" a","(a)");

test_addParen(" b","(b)");

test_addParen(" ab","(a)(b)");

test_addParen(" ; Hello!","(H)(e)(l)(l)(o)(!)");

std :: printf(" working\\\
" );

}


运行上述内容,直到输出正在工作。如果您在使用

时遇到问题,请在addParen(char *)

函数中向我们展示您目前所拥有的内容以及错误是什么,我们我会帮你的。

-

魔术取决于传统和信仰。它不欢迎观察,

也不会通过实验获利。另一方面,科学的经验基于
;它可以通过观察和实验进行校正。



I would first complain. How much memory has been allocated to the str
being passed in? What if it can''t handle all the extra characters I need
to add to it? It''s a very dangerous function signature.

that said, you need to set up some tests:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void addParen(char* str)
{
// put your code here
}

void test_addParen( const string& input, const string& expected ) {
std::vector<char> str( input.size() * 3 + 1 );
copy( input.begin(), input.end(), str.begin() );

addParen( &str[0] );

if ( expected != &str[0] ) {
cout << "Failed test for input ''" << input
<< "'': expected ''" << expected
<< "'' but got ''" << &str[0] << "''\n";
throw -1;
}
}

int main()
{
test_addParen( "a", "(a)" );
test_addParen( "b", "(b)" );
test_addParen( "ab", "(a)(b)" );
test_addParen( "Hello!", "(H)(e)(l)(l)(o)(!)" );
std::printf( "working\n" );
}

Run the above until it outputs "working". If you are having trouble with
something, show us what you have so far in the "addParen(char*)"
function and what the error is and we''ll help you out.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.


这篇关于C / C ++ char指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆