You are on page 1of 5

21/4/2016

CLanguage,Needhelpwithstringfunctions:LEFT,RIGHTandMID

BoardindexCLanguage

AlltimesareUTC

Needhelpwithstringfunctions:LEFT,RIGHTandMID
Need help with string functions: LEFT, RIGHT and MID
Author

Wood..
#1/6

Message

Needhelpwithstringfunctions:LEFT,RIGHTandMID

Hello,
Ineedsomehelphere!IamputtingtogetherapieceofCcode,
and
needsomefunctionswhichdothesamethingasothersIhave
come
acrossinOPLandVisualBasic.
Thefunctionsarestringhandlingfunctionsandareoftencalled
somethinglike:
leftstr(char*str,intn)returnstheleftncharsofstrasastring
rightstr(char*str,intn)returnstheleftncharsofstrasa
string
midstr(char*str,intn,inti)returnsthencharsofstr,starting
at
positioni
rightstriseasytowrite(Ithink),butcananyonehelpmeout
with
theothers?Thesefunctionswouldbeinvaluableastheywould
allowme
tomanipulatestringswihtouthavingtomakealocalcopy(the
strings
inquestioncanbeanylength).
Manythanks,
David.
SentviaDeja.comhttp://www.***.com/
Beforeyoubuy.
Tue,30Apr200203:00:00GMT

JoonaIPalast
#2/6

Needhelpwithstringfunctions:LEFT,RIGHTandMID

:Hello,
:Ineedsomehelphere!IamputtingtogetherapieceofC
code,and
:needsomefunctionswhichdothesamethingasothersIhave
come
:acrossinOPLandVisualBasic.
:Thefunctionsarestringhandlingfunctionsandareoftencalled
:somethinglike:
:leftstr(char*str,intn)returnstheleftncharsofstrasa
string
:rightstr(char*str,intn)returnstheleftncharsofstrasa
string
Umm...whatwouldbethedifferencebetweenleftstrand
http://computerprogrammingforum.com/47clanguage/d8e8fcbcd674199b.htm

1/5

21/4/2016

CLanguage,Needhelpwithstringfunctions:LEFT,RIGHTandMID

rightstr?
:midstr(char*str,intn,inti)returnsthencharsofstr,starting
at
:positioni
:rightstriseasytowrite(Ithink),butcananyonehelpmeout
with
:theothers?Thesefunctionswouldbeinvaluableastheywould
allowme
:tomanipulatestringswihtouthavingtomakealocalcopy(the
strings
:inquestioncanbeanylength).
:Manythanks,
:David.
Thesekindsoffunctionsareeasytowrite.Iftheyreturnanew
copy,
youhavetomallocitinsidethefunction,orpassthepointerto
the
newcopyasaparameter.Infactgettingtheleftncharsis
easier
thangettingtherightnchars.Intheleftncase,juststartatthe
beginning,keepcopyingcharsuntilyouhitn,andputa'\0'at
the
end.Intherightncase,youhavetousestrlen()orsomething
similar
tofindoutthelengthofthestring,thensubstractthingstoget
the
rightstartingposition.
Andthemidncasecanbeexpressedasacombinationofthe
leftnand
rightncases.

|Kingpriestof"TheFlyingLemonTree"G++FRFW+M#80
D+ADAN+++|
|http://www.helsinki.fi/~palasteW++BOP+
|
\Finlandrules!/
Tue,30Apr200203:00:00GMT

MikeSalte
#3/6

Needhelpwithstringfunctions:LEFT,RIGHTandMID

Quote:

>Ineedsomehelphere!IamputtingtogetherapieceofCcode,and
>needsomefunctionswhichdothesamethingasothersIhave
come
>acrossinOPLandVisualBasic.

>Thefunctionsarestringhandlingfunctionsandareoften
called
>somethinglike:
>leftstr(char*str,intn)returnstheleftncharsofstrasa
string
>rightstr(char*str,intn)returnstheleftncharsofstrasa
string
>midstr(char*str,intn,inti)returnsthencharsofstr,
startingat
>positioni

http://computerprogrammingforum.com/47clanguage/d8e8fcbcd674199b.htm

2/5

21/4/2016

CLanguage,Needhelpwithstringfunctions:LEFT,RIGHTandMID

Leftstr()

where:
#include<string.h>
charnewstr[bigenuff]
chars[somesize]
size_tlft=5/*numberofcharstocopy*/

strncpy(newstr,s,lft)
newstr[lft]='\0'
Rightstr()

size_trt=5
strcpy(newstr,(s+(strlen(s)rt+1)))
Midstr()

intstart=3/*startingposition*/
size_tlen=5/*numofcharstocopy*/
strcpy(newstr,(s+(start1)),len)
Ibelievethiswouldgiveyouwhatyouwouldwant(althoughtI
haven't
compiledhiscode).Readthedocsforstrlen(),strcpy()and
strncpy().

Tue,30Apr200203:00:00GMT

MikeSalte
#4/6

Needhelpwithstringfunctions:LEFT,RIGHTandMID
MS>Midstr()
MS>
MS>intstart=3/*startingposition*/
MS>size_tlen=5/*numofcharstocopy*/
MS>
MS>strcpy(newstr,(s+(start1)),len)

SorryIhitentertooquick.Thatlastlineshoudusestrncpy().
andbefollowedbynewstr[len]='\0'
Idon'tneedmorecoffee,doI!
Mike
Tue,30Apr200203:00:00GMT

AlBower
#5/6

Needhelpwithstringfunctions:LEFT,RIGHTandMID
Quote:

>>midstr(char*str,intn,inti)returnsthencharsofstr,starting
at
>>positioni

....snip..........
Quote:

>Midstr()
>
>intstart=3/*startingposition*/
>size_tlen=5/*numofcharstocopy*/

>strcpy(newstr,(s+(start1)),len)
>Ibelievethiswouldgiveyouwhatyouwouldwant
(althoughtIhaven't
>compiledhiscode).Readthedocsforstrlen(),strcpy()
http://computerprogrammingforum.com/47clanguage/d8e8fcbcd674199b.htm

3/5

21/4/2016

CLanguage,Needhelpwithstringfunctions:LEFT,RIGHTandMID

andstrncpy().
Thedocssaythatfunctionstrcpytakestwoarguments.
YoucouldusestrncpylikeyoudidwithLeftstr().

Butjudgingfromtheoriginalpost,thefunctionwillmodifystr
andreturn
it.Theposterused
char*strandnotconstchar*str.Inthiscaseyouneedtouse
memmove.

AlBowers
Tampa,FLUSA
http://www.gate.net/~abowers/

Tue,30Apr200203:00:00GMT

BurakSerda
#6/6

Needhelpwithstringfunctions:LEFT,RIGHTandMID
Quote:

>Hello,

>Ineedsomehelphere!IamputtingtogetherapieceofC
code,and
>needsomefunctionswhichdothesamethingasothersI
havecome
>acrossinOPLandVisualBasic.
>Thefunctionsarestringhandlingfunctionsandareoften
called
>somethinglike:
>leftstr(char*str,intn)returnstheleftncharsofstrasa
string
>rightstr(char*str,intn)returnstheleftncharsofstrasa
string
>midstr(char*str,intn,inti)returnsthencharsofstr,
startingat
>positioni
>rightstriseasytowrite(Ithink),butcananyonehelpme
outwith
>theothers?Thesefunctionswouldbeinvaluableasthey
wouldallowme
>tomanipulatestringswihtouthavingtomakealocalcopy
(thestrings
>inquestioncanbeanylength).
Whatwillbethereturnvalue?Willitdirectlymodifythegivenstring,
orwillitallocatesomememoryandreturnanewstring?

Ifitdirectlymodifiesthegivenstring,formidstr:
memmove(str,str+i,n)
str[n]=0
woulddothejob.Ifitisrequiredtoallocatenewspace:
char*buf=malloc(n+1)
memcpy(buf,str+i,n)
buf[n]=0
isok.Youshouldcheckvalidityofnandiinadvance.Theothers
are
specialcasesofthis.
http://computerprogrammingforum.com/47clanguage/d8e8fcbcd674199b.htm

4/5

21/4/2016

CLanguage,Needhelpwithstringfunctions:LEFT,RIGHTandMID

Tue,30Apr200203:00:00GMT

Page1of1 [6post]

Relevant Pages

1.stringfunktionlikeright,left,mid
2.stringequivalenttoCStrings.Leftand.Right
3.midfunctionforwidestrings?
4.righttolefttraversaltwalkfunction?
5.TheRight/LeftRuleforverbalizingfunctiondeclarations?
6.Basicleft$andright$functions??

7.RightLeftRule...PleaseHelp
8.newbieneedshelptostrings,strings,strings
9.NeedHelpwithStringFunction
10.RighttrimstringfunctioninC
11.NeedhelpwithNetMeetingSDK...INMConference.Leave()
12.RightClickvs.Left

PoweredbyphpBBForumSoftware

http://computerprogrammingforum.com/47clanguage/d8e8fcbcd674199b.htm

5/5

You might also like