Extracting a substring

Hello. This is a basic question, but I haven't been able to find out
the answer by my own.

I have a variable which is a char array, I mean:


    char line_p[80] ;


How can I get the substring from the 1st character to the 10th (for
example) and store it in another char array ??

I've found the method "substring" or something similar, but that's for
"string" objects, which is not my case.

Thank you in advance!

--
Vicent
0
vginer (51)
1/20/2010 2:50:44 PM
comp.lang.c++ 48802 articles. 0 followers. Post Follow

3 Replies
309 Views

Similar Articles

[PageSpeed] 15
On Jan 20, 6:50=A0am, Vicent Giner-Bosch <vgi...@gmail.com> wrote:
> Hello. This is a basic question, but I haven't been able to find out
> the answer by my own.
>
> I have a variable which is a char array, I mean:
>
> =A0 =A0 char line_p[80] ;
>
> How can I get the substring from the 1st character to the 10th (for
> example) and store it in another char array ??
>
> I've found the method "substring" or something similar, but that's for
> "string" objects, which is not my case.
>
If you really must use a char array (C-style string) instead of a
string, look at these:
strncpy
memcpy
memmove

--
Fred K
0
Fred
1/20/2010 3:25:31 PM
> If you really must use a char array (C-style string) instead of a
> string, look at these:
> strncpy
> memcpy
> memmove

Well, I prefer using C-style strings.

I've understood the use of strncpy, thanks.

Two more questions:

(1) What if I want to get the substring from the 3rd to the 7th
position (for example)?

(2) Is there any "wrap" function for C-style strings, I mean, any
function for taking away the white spaces at the beginning at the end
of a string?

Thank you in advance.

--
Vicent
0
Vicent
1/20/2010 4:20:04 PM
On 2010-01-20, Vicent Giner-Bosch <vginer@gmail.com> wrote:
>
>> If you really must use a char array (C-style string) instead of a
>> string, look at these:
>> strncpy
>> memcpy
>> memmove
>
> Well, I prefer using C-style strings.
>

Then use C. C++ has much stricter typing than C, which
forces you to cast the return value of malloc (a risky
practice), and in general use more casts than is safe.

Also, you have to track string length yourself, which
breaks encapsulation and is also often slower.

0
Andrew
1/20/2010 4:35:29 PM
Reply:
Similar Artilces:

Re: errors by function SUBSTR #2
Tony, What you are asking for is good old defensive programming. If one does not care to have some value in each value for variable 'a' but would like to know when a word does not meet the specifications, then you could do the following. data sub ; set one ; length a $12. ; if length(word) >= 12 then do ; a = substr(word,3,12) ; end; else put word = ' is either blank or not long enough for the substr' ; run ; HTH Toby Dunn -----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Tony Wang Sent: Tuesday, September 28, 2004 1:52...

Extract Number From Text String
Hello, Does xHarbour have a function to extract Number from a text string? Regards Otto Dear Otto Haldi: On Monday, September 17, 2012 11:48:58 AM UTC-7, Otto Haldi wrote: .... > Does xHarbour have a function to extract > Number from a text string? http://www.ousob.com/ng/53guide/nga7e04.php val() David A. Smith On Mon, 17 Sep 2012 12:05:13 -0700 (PDT), dlzc <dlzc1@cox.net> wrote: Hello David, Thanks for your help. Yes, I know the function val() I mean for that kind of string : "abc-2344-2323". Regards Otto >Dear Otto Haldi: >...

Extracting records
Hi, I am looking for a solution to this problem. I receive a list of records like this, in one continuous string. I want to separate each record and extract some of the data for each record. The records look like this: (=YEAR:1991;month:[JAN]client-NAME:[Ali-Baba-Basket-Emporium]AREA:[SouthEast]value:[�1905]=)(=YEAR:1997;month:[dec]client-NAME:[Fletcher]AREA:[Scotland(North)]discount:[7%]value:[�741]=)(=YEAR:2003;month:[MAR]client-NAME:[Porridge-dot-com]AREA:[N.Ireland]discount:[7%]value:[�335]=) For example, in this record: (=YEAR:2003;month:[MAR]client-NAME:[Porridge-dot-com]AREA:[N.Irelan...

extract values from a csv file
Hi All, I would like to know how to extract values using shell script from csv files and store the value in variables and then display the values I need something like this var1 = a var2 = 230 var3 = david var4 = 450 and then display the values where csv file has ab,230,david,450 gh,430,jack,678 aa,560,sam,340 Thanks Deva "deva" <devasingh@gmail.com> wrote in message news:1117867246.217480.268120@g47g2000cwa.googlegroups.com... > Hi All, > > I would like to know how to extract values using shell script from > csv files and store the value...

Extracting text from a webpage
Hello All, This must be an easy question for many of you. Given its URL, I would like to extract the text from a webpage **after the server and client side updates are done**. Can LWP::Simple be used for this? Thanks. q-rious <mittra@juno.com> wrote: >This must be an easy question for many of you. Given its URL, I would >like to extract the text from a webpage **after the server and client >side updates are done**. Please explain what you mean by "server and client side updates". >Can LWP::Simple be used for this? Yes and no. You can certainly use LWP::Simple...

Extract JEditorPane from swing jar
Hi, I'm developing an applet which need to use JEditorPane, but I don't want to use plug-in with browser. In my understanding, the solution is extracting all classess from swing jar and pack them into another jar along with my applet. The problem is I don't know which classes should I extract from swing jar to make JEditorPane works. Am I on the right way? Please help me !!! thanks, Thang. "Thang Nguyen" wrote: > Am I on the right way? Please help me !!! No, many before you had that stupid idea. ...

extracting foreground_blobs
hello, in the following image there is a truck and a car. i appear to be intutive can some body please exaplin me how to obtain them (something like extacting one big blob of each of the vehicle.) hello, in the following image there is a truck and a car. it appear to be intutive but i am not able to code to extract them. can some body please exaplin me how to obtain them (something like extacting one big blob of each of the vehicle.) image: (https://docs.google.com/file/d/0BxDBJuGxr2fJWmVJYVhZMDNoR28/edit) ...

SUbstring Extraction
I have a program that reads input from the user in the form Name("ABC") Addr("DEF") I need to check if Name was mentioned and extract ABC and store it in a variable and if Addr was mentioned and extract DEF and store that in another variable. Eg: if the user inputs Name("Joe") I need to check if he has mentioned Name and extract Joe The user can input Name("Joe") Addr("unit 2 sheperton") in this case i need to extract Joe and unit 2 sheperton and perform the appropirtae functions depending on whether Name or Addr or Both are mentioned. Tha...

The Asian Leading Aloe Extract Manufacturer
We are the largest aloe vera raw materials manufacturer in Asia. We are engrossed in integrated supply of raw Aloe Vera materials. All of our Aloe Vera gels, Aloe Vera whole leaf, concentrates and powders are tested and guaranteed to be biologically active. We had our entire product line certified by the International Aloe Science Council (IASC). We have the following advantages: 1. The Asian largest aloe plantation base. 2. 5 state-to-the-art processing lines(the most in the world) certified by IASC. 3. Standard GMP production workshops. 4. Low-temprerature concentration technology 5. World ...

Ideas for extracting fibers
Hey, I'm working on a problem where I have an image of two strings laid across each other and I have to determine quantitative data about the two strings. How might I single each string out? Thanks On Jun 10, 10:22=A0am, "Victor " <a...@aoeu.com> wrote: > Hey, > I'm working on a problem where I have an image of two > strings laid across each other and I have to determine > quantitative data about the two strings. =A0How might I single > each string out? > > Thanks ---------------------------------------------------------------------- Victor: Can...

extracting function from the list
Hi, all: I can not seem to figure out how to extract a function from the list. for example: mathematica gives me output: {{v->x^2}} How do I extract just x^2 out of that list and assign it to a different variable? thank you, edgar Lobachevskiy Hi, RelaceAll[] my help you, in the case where you can have more than a single solution you have to decide which solution you whant to use and q= v /. {{v->x^2}}[[1]] will help you. Regards Jens edgar wrote: > > Hi, all: > > I can not seem to figure out how to extract a function from the li...

hang-up..Extracting..Extracting
I installed Sol8/Intel on 160 GB . only 131 GB were visible to Sol though I have installed the ata batch before installation I do not know why ?? Any way the PC hang up after the the web start launcher and sol said Extracting Extracting and machine goes into sleep before the Login ? Extracting what ?? What is happening . I always forced to reset machine ? ehabaziz2001@gmail.com wrote: > I installed Sol8/Intel on 160 GB . only 131 GB were visible to Sol > though I have installed the ata batch before installation I do not know > why ?? > Any way the PC hang up after the the web st...

Road Extraction
Hello friends, I have a project named road extraction, we have to extract roads from different images, There are lots of different ways for doing this but I have no time so I need the best way, any Idea please. Also how can I find sample source codes for understanding better. Best Regards, Fariba On Jun 5, 3:59=A0pm, "Fariba Yousefi" <fariba_...@yahoo.co.uk> wrote: > Hello friends, > > I have a project named road extraction, we have to extract roads from dif= ferent images, There are lots of different ways for doing this but I have n= o time so I need the best way, ...

how to extract
hai<font>hai</font>hello<font> hello</font>india<font>welcome </font>jdfkd how do i get following type of output from above the input? <font>hai</font> <font>hello</font> <font>welcome</font> On Thu, 26 Feb 2004 02:03:52 -0600, kums wrote: > hai<font>hai</font>hello<font> > hello</font>india<font>welcome > </font>jdfkd > > how do i get following type of output from above the input? > <font>hai</font> > <font>hello</font> > <fon...

How to extract a vectorial image from pdf
Hi I would like to extract a vectorial figure from a pdf. Since there is text in this figure and I want to print it latter, I need to extract it and preserve its vectorial structure. Do some tools exist to do taht? Thanks Ludovic Martin wrote: > Hi > > I would like to extract a vectorial figure from a pdf. Since there is text > in this figure and I want to print it latter, I need to extract it and > preserve its vectorial structure. > Do some tools exist to do taht? > > Thanks > > Adobe Illustrator is one tool that will do it. You can also save the...

Extracting graphics from notebook
Hi, Can anyone suggest how I can address a graphics object from a saved notebook. In the example below the most I can do is to CellPrint the object, but I would like to set it to a variable. dir = Directory[]; nb1 = CreateDocument[Plot[Sin[x], {x, 0, 2 Pi}], NotebookFileName -> dir <> "\\" <> "Sin2Pi.nb"]; NotebookSave[nb1]; NotebookClose[nb1]; nb2 = NotebookOpen[dir <> "\\" <> "Sin2Pi.nb"]; SelectionMove[nb2, Next, Cell] CellPrint[NotebookRead[nb2]] NotebookClose[nb2]; Am 11.01.2011 06:35, sc...

Surface Extraction
Hi all, If you can help with the following that would be very useful; I have several interpolated surfaces of the same region, however they are on slightly different grids (as the loess interpolation uses the min and max of the data set) and so the X,Y positions vary. I want to compare the change in the surfaces i.e. the Z values, however i obviously need corresponding X,Y positions for each surface. To do this is have simply done the following; [Xg,Yg]= meshgrid(XX , YY); %Defined a grid based on pre-set co-ords Zg = griddata (Xi,Yi,Zi,Xg,Yg); %Ext...

getting substrings and assigning to array
hi all! I have a question folks! Given: test1="|hello world|abc" Problem: Need to produce the output arr[0]=hello world arr[1]=abc Can someone please tell me how to do this? I also have another question which is more cost efficient. nested loops or unix utilities within nested loops? thanks so much! melissa_benkyo wrote: > Given: test1="|hello world|abc" > Problem: > Need to produce the output > arr[0]=hello world > arr[1]=abc with ksh93, bash: IFS='|'; set -f arr=( ${test1#'|'} ) With zsh (beware that arrays in...

Statistics Extraction
Hi, I wanted to extract specific information of MAC and PHY statistics from the statistics log file. The Statistics file contains information other than MAC and PHY also. I am mainly concerned about "frames with invalid header" and "packets with invalid length" from the MAC Statistics and "transmit errors" from PHY Statistics. Can anyone let me know how can I extract this information. Here is a snapshot of the Statistics file that gets generated. MAC statistics: 0 frames with invalid header 546 confirmed frames sent succesfully 1693 con...

Extracting audio from DVDs
What is the best way of extracting the soundtracks from a DVD? I have come across a few answers in the net, but most of them are either vague (we are told that it can be done, but the details are omitted) or so obsolete that they do not seem to work properly with current tools. Also, they do not seem in general to address the issue that a DVD usually contains several soundtracks. In news:pan.2006.11.19.05.39.05.214814@story.net, Augustus SFX van Dusen <ASFXvD@story.net> wrote: > What is the best way of extracting the soundtracks from a DVD? I have > come across a few answers...

How can extract a manifold mesh from a non-manifold triangles?
I'm searching for such an algorithm, it can generate a non-manifold mesh from a set of triangles. The input triangles are on the object's surface, and connected together. Part of the mesh (about 20%) is non-manifold. So I want to find a way to deleted some triangles, and convert the input mesh into manifold mesh (needn't to be bounded) by delete bad triangles. Is there any algorithm satissified this requirement? Keith.Lv wrote: > The input triangles are on the object's surface, and connected > together. Part of the mesh (about 20%) is non-manifold. Please try to be ...

have a short, need to extract 8 bits so I can have a char
Hola! I have an array of shorts that represents a stream of data. This data is packed in tight, so certain elements may actually contain data for more than one variable. I am to decipher this stream. The first variable packed into this array happens to be a short, meaning I get away with a simple assignment: myFirstVariable=myArray[0]; My problem is that mySecondVariable is a char. What that means is that the char I need is making up 50% of myArray[1]. How can I get those 8 bits, and only those 8 bits? This is a pretty big sticking point, because it follows naturally that myThirdVariabl...

Extracting a surface in a binary volume
Hello, I have a simple problem which I am hoping to find a simple solution for. I have a binary volume where elements that represent the object are 1 and the background is 0. I would like to extract the TOP surface of the object. For example, if the object was a solid sphere, I would want to extract the surface of the topmost hemisphere. I have previously achieved this by having nested for loops which search each line of each slice of the volume and make the first '1' element encountered '1' in the corresponding voxel in a new empty volume then exiting the for loop. S...

question about extracting value from a string
hey there, i have looked at the string module and re. i was looking for advice on what would be the best way to pull a value out of a small string. for example, i have a string $.+.09 JAR and all i want out of it is the +.09 likewise, i have $-.04 TIN kt and all i want is the -.04 what would be my best bet here ? do i need a full blown re comparison? thanks <nephish@xit.net> wrote: > hey there, > > i have looked at the string module and re. > i was looking for advice on what would be the best way to pull a value > out of a small string. > > for exampl...

Word extraction
How could i find out any colum with particular string of word. e.g. - last word first word last train movie movie last signal last I would like to have only colums with word ---- Last Thanks P ...