得到一个字符串的中间 [英] Getting the middle of a string

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

问题描述

我试图创建一个返回字符串中间的函数。对于

示例:

strmid(这是一个文本,6,4);将返回是一个。


这是我的代码:

char * strmid(char * texte,int depart,int longueur)

{char * resultat =" " ;;

char * temporaire =" " ;;

int nbr;


nbr = depart + longueur - 1;


strncat(temporaire, texte,nbr);

strrev(temporaire);

strncat(resultat,temporaire,longueur + 1);

strrev(resultat);


返回结果;

}

它无法正常工作,但我真的不明白为什么。谁能

赐教我?或者,如果有类似的代码已经可以使用,我可以请

吗?


谢谢,

- 新的C程序员

解决方案

" Olivier Bellemare" <醇*************** @ cgocable.ca>写道:

char * strmid(char * texte,int depart,int longueur)
{char * resultat =" " ;;
char * temporaire =" " ;;
int nbr;

nbr = depart + longueur - 1;

strncat(temporaire,texte,nbr);
strrev(temporaire );
strncat(resultat,temporaire,longueur + 1);
strrev(resultat);

返回结果;
}




根据该代码,我建议您在开始编程时购买一本关于

的书。我不认为你准备好了这个

新闻组。很多

类别中的错误太多。

-

我没有C& V方便,但我有Dan Pop。

--E。 Gibbons





Olivier Bellemare写道:

我试图制作一个函数返回字符串的中间部分。对于
示例:
strmid(这是一个文本,6,4);将返回is a。

这是我的代码:
char * strmid(char * texte,int depart,int longueur)
{char * resultat =" " ;;
char * temporaire =" " ;;
int nbr;

nbr = depart + longueur - 1;

strncat(temporaire,texte,nbr);
strrev(temporaire );
strncat(resultat,temporaire,longueur + 1);
strrev(resultat);

返回结果;
}

它不能正常工作,但我真的不明白为什么。任何人都可以开导我吗?或者,如果有类似的代码已经可以使用,我可以请它吗?

谢谢,
- 一位新的C程序员




你在弦的中间是什么意思?您提供的示例是

有点不清楚。


Olivier Bellemare写道:

我试图创建一个返回字符串中间的函数。对于
示例:
strmid(这是一个文本,6,4);将返回is a。

这是我的代码:
char * strmid(char * texte,int depart,int longueur)
{char * resultat =" " ;;
char * temporaire =" " ;;
int nbr;

nbr = depart + longueur - 1;

strncat(temporaire,texte,nbr);
strrev(temporaire );
strncat(resultat,temporaire,longueur + 1);
strrev(resultat);

返回结果;
}

它不能正常工作,但我真的不明白为什么。任何人都可以开导我吗?或者,如果有类似的代码已经可以使用,我可以吗?




请记住C下标从零开始。


如何(未经测试):


#include< string.h>

char * mid(char * texte,size_t depart,size_t longueur)

{char * resultat;

size_t maximum = strlen(texte) - 离开;

if(b) longeur> maximum)longeur = maximum;

resultat = malloc(longueur + 1);

if(resultat)

{memcpy(resultat) ,texte + depart,longueur);

resultat [longueur] =''\ 0'';

}

返回结果; < br $>
}


别忘了检查返回值(如果内存不能为
分配,则为NULL结果);当你完成它后,释放()分配的存储




-

Morris Dovey

美国爱荷华州西得梅因

C链接 http://www.iedu.com/c

读我的嘴唇:苹果离树不远。


I''ve tried to make a function that returns the middle of a string. For
example:
strmid("this is a text",6,4); would return "is a".

Here is my code:
char *strmid(char *texte, int depart, int longueur)
{ char *resultat = " ";
char *temporaire = " ";
int nbr;

nbr = depart + longueur - 1;

strncat(temporaire,texte,nbr);
strrev(temporaire);
strncat(resultat,temporaire,longueur+1);
strrev(resultat);

return resultat;
}
It doesn''t work properly, but I really don''t understand why. Can anyone
enlighten me? Or, if there''s a similar code that works already, can I have
it please?

Thanks,
- A new C programmer

解决方案

"Olivier Bellemare" <ol***************@cgocable.ca> writes:

char *strmid(char *texte, int depart, int longueur)
{ char *resultat = " ";
char *temporaire = " ";
int nbr;

nbr = depart + longueur - 1;

strncat(temporaire,texte,nbr);
strrev(temporaire);
strncat(resultat,temporaire,longueur+1);
strrev(resultat);

return resultat;
}



On the basis of that code, I''d recommend that you buy a book on
beginning C programming. I don''t think you''re ready for this
newsgroup yet. There are just too many errors in too many
categories.
--
"I don''t have C&V for that handy, but I''ve got Dan Pop."
--E. Gibbons




Olivier Bellemare wrote:

I''ve tried to make a function that returns the middle of a string. For
example:
strmid("this is a text",6,4); would return "is a".

Here is my code:
char *strmid(char *texte, int depart, int longueur)
{ char *resultat = " ";
char *temporaire = " ";
int nbr;

nbr = depart + longueur - 1;

strncat(temporaire,texte,nbr);
strrev(temporaire);
strncat(resultat,temporaire,longueur+1);
strrev(resultat);

return resultat;
}
It doesn''t work properly, but I really don''t understand why. Can anyone
enlighten me? Or, if there''s a similar code that works already, can I have
it please?

Thanks,
- A new C programmer



What do you mean by middle of a string? The example you''ve provided is a
little unclear.


Olivier Bellemare wrote:

I''ve tried to make a function that returns the middle of a string. For
example:
strmid("this is a text",6,4); would return "is a".

Here is my code:
char *strmid(char *texte, int depart, int longueur)
{ char *resultat = " ";
char *temporaire = " ";
int nbr;

nbr = depart + longueur - 1;

strncat(temporaire,texte,nbr);
strrev(temporaire);
strncat(resultat,temporaire,longueur+1);
strrev(resultat);

return resultat;
}

It doesn''t work properly, but I really don''t understand why. Can anyone
enlighten me? Or, if there''s a similar code that works already, can I have
it please?



Remember that C subscripts start at zero.

How about something like (untested):

#include <string.h>
char *mid(char *texte,size_t depart,size_t longueur)
{ char *resultat;
size_t maximum = strlen(texte) - depart;
if (longeur > maximum) longeur = maximum;
resultat = malloc(longueur + 1);
if (resultat)
{ memcpy(resultat,texte + depart,longueur);
resultat[longueur] = ''\0'';
}
return resultat;
}

Don''t forget to check the return value (NULL if memory couldn''t
be allocated for the result); and to free() the allocated storage
when you''re done with it.

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn''t fall far from the tree.


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

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