字符串反向问题 [英] String Reverse Question

查看:87
本文介绍了字符串反向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这个函数来反转给定的字符串。

我只是好奇,如果这是正确的,可能有更好的方式

这样做/可能的错误相同。


函数原型类似于任何标准C

库中的函数原型。

< ----代码开始 - >


char * my_strrev(char * mine){

static char * p = NULL; //确保指针不会在返回时损失


char * q;

int len;

int i;


len = strlen(我的);

p =新字符[len + 1];

// 1代表''\ 0''字符


q =我的;

i = len - 1;

而( * q){

*(p + i)= * q ++;

i - ;

}

*(p + len)=''\''';

返回p;

}


< - 代码结束 - >


- Rakesh。

Hi,
I have this function to reverse the given string.
I am just curious if that is correct and there could be better way of
doing it / probable bugs in the same.

The function prototype is similar to the one in any standard C
library.
<---- Code starts -->

char * my_strrev(char * mine) {
static char * p = NULL; // To make sure that the pointer is not
lost on return.
char * q;
int len;
int i;

len = strlen(mine);
p = new char[len + 1];
// 1 for ''\0'' character

q = mine;
i = len - 1;
while (*q) {
*(p + i) = *q++;
i--;
}
*(p + len ) = ''\0'';
return p;
}

<-- Code Ends -->

- Rakesh.

推荐答案



" Rakesh" <博士******** @ yahoo.com> écritdansle message de

news:3e ************************** @ posting.google.c om ...

"Rakesh" <dr********@yahoo.com> a écrit dans le message de
news:3e**************************@posting.google.c om...





我有这个函数来反转给定的字符串。
我我只是好奇,如果这是正确的,可以有更好的方式来做/它可能的错误。

函数原型类似于任何标准C中的那个
库。

< ----代码开始 - >

char * my_strrev(char * mine){
静态字符* p = NULL ; //确保指针在返回时不会丢失。
char * q;
int len;
int i;

len =的strlen(矿);


我的可能是NULL并导致段错误。还要小心我的是空的

终止。


你可以将它放在strlen调用之上:

if(mine = = NULL)返回NULL;

p = new char [len + 1];


新?它看起来像C ++,而不是C.使用malloc,calloc或realloc。


p = malloc(len + 1);


if (p)

{

// 1为''\ 0''字符

q =我的;
i = len - 1;
while(* q){
*(p + i)= * q ++;
i--;
}
*(p + len )=''\''';


}

返回p;
}

< - 代码结束 - >


Regis
- Rakesh。
Hi,
Hi,
I have this function to reverse the given string.
I am just curious if that is correct and there could be better way of
doing it / probable bugs in the same.

The function prototype is similar to the one in any standard C
library.
<---- Code starts -->

char * my_strrev(char * mine) {
static char * p = NULL; // To make sure that the pointer is not
lost on return.
char * q;
int len;
int i;

len = strlen(mine);
mine could be NULL and cause a segfault. Be also careful that mine is null
terminated.

You can put this above the strlen call :
if (mine==NULL) return NULL;
p = new char[len + 1];
new ?? it looks like C++, not C. Use malloc, calloc, or realloc instead.

p = malloc(len+1);

if (p)
{
// 1 for ''\0'' character

q = mine;
i = len - 1;
while (*q) {
*(p + i) = *q++;
i--;
}
*(p + len ) = ''\0'';
}
return p;
}

<-- Code Ends -->
Regis
- Rakesh.



"RégisTroadec" <重** @ wanadoo.fr>在消息新闻中写道:< c5 ********** @ news-reader1.wanadoo.fr> ...
"Régis Troadec" <re**@wanadoo.fr> wrote in message news:<c5**********@news-reader1.wanadoo.fr>...
我的可能是NULL并导致段错误。小心我的是空的终止。


哦,是的 - 非常感谢。这是非常重要的。但只是一个设计q。

这里 - 我应该处理这个/我应该把它留给库的

用户,以确保不传递NULL对于这个。

哪一个看起来最好。


你可以把它放在strlen调用之上:
if(mine == NULL)return NULL;
mine could be NULL and cause a segfault. Be also careful that mine is null
terminated.
Oh Yeah - Thanks a ton. This is very important. But just a design q.
here - am i supposed to handle this one / should i leave it to the
user of the library to make sure that NULL is not passed to this.
Which one seems the best.


You can put this above the strlen call :
if (mine==NULL) return NULL;
p = new char [len + 1];
p = new char[len + 1];



new ??它看起来像C ++,而不是C.使用malloc,calloc或realloc。



new ?? it looks like C++, not C. Use malloc, calloc, or realloc instead.




哎呀!我的错 。最近,我在C ++中做了很多,而且b / b因此出现了问题。


感谢您的评论,Régis



Oops ! My mistake . Of late, I had been doing a lot in C++ and
hence the problem.

Thanks for your comments, Régis


Rakesh< dr ******** @ yahoo.com>这样说:
Rakesh <dr********@yahoo.com> spoke thus:
char * my_strrev(char * mine){
const char * mine

static char * p = NULL; //确保返回时指针不会丢失。


1)不要将c ++风格的评论发布到comp.lang.c(更晚些时候)

2)错位的问题 - 返回指针值因此是没有丢失的b $ b。

char * q;


无害但不必要 - 你可以简单地使用我而不是

声明另一个变量。

int len;
int i;
len = strlen(mine);
p = new char [len + 1];


这个(新的)是C ++。不要在这里发布。 ITYM


p = malloc(len * sizeof(* p)+1);

// 1为''\ 0''字符


你记得很清楚。

q =我的;
我= len - 1;
而(* q){
*(p + i)= * q ++;
i--;
}
*(p + len)=''\ 0'';
返回p;
}
char * my_strrev(char * mine) { const char *mine
static char * p = NULL; // To make sure that the pointer is not
lost on return.
1) Don''t post C++ style comments to comp.lang.c (more later)
2) Misplaced concern - the pointer value is returned and thus is
not lost.
char * q;
Harmless but unnecessary - you can simply use mine rather than
declaring another variable.
int len;
int i; len = strlen(mine);
p = new char[len + 1];
This (new) is C++. Don''t post it here. ITYM

p=malloc( len*sizeof(*p)+1 );
// 1 for ''\0'' character
You did well to remember it.
q = mine;
i = len - 1;
while (*q) {
*(p + i) = *q++;
i--;
}
*(p + len ) = ''\0'';
return p;
}




-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于字符串反向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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