CompuServe Thread

#mid$ like function in C

13 messages in this thread
#119120From: Robert BrownApr 13, 1988 8:27 PM
I am new to C programing, and am looking for a source code of a mid$ like function. I have looked on the DL section and have found nothing even close. Any help would be considered a blessing!
#119121From: Steve AhlstromApr 13, 1988 8:32 PM
I can't help you there. I never used a BASIC that had a mid$ function. What is it?
#119124From: Lloyd W. Dull IIIApr 13, 1988 8:48 PM
Steve, MID$ is pretty standard in BASIC's. PRINT MID$("Computer",4,3) would print "put" – the three MIDdle characters that start 4 characters from the left. Don't know much about C, however. Lloyd
#119131From: Steve AhlstromApr 13, 1988 9:05 PM
Ah, ok. That's easy in C.
#119169From: Vic WagnerApr 13, 1988 11:08 PM
Well, it's not as easy as it looks. If you're going to make a valid 'C' string out of it, you have to put the trailing '\000' on it. And a proper definition of mid is: char *mid(string, first, length) char *string; unsigned int first, length; So, either you gotta destroy the input string to get the correct terminator on it. Or you gotta allocate some memory to put the newly created string in (who is going to de-allocate it??). This is why most string handling packages in C don't have it.
#119213From: Richard Rae/SYSOPApr 14, 1988 8:26 AM
<Ahem> MID$ will also handle two arguments and gives you the string from the first argument to the end of the string, which is distinct from the function of RIGHT$. MID$ can ALSO be used on the LEFT side of the assignment to REPLACE characters in a string. Therefore MID$("THIS IS A TEST",2,3)="HAT" will result in "THAT IS A TEST" (assuming you could imbed a literal like that and then DO anything with it). MID$ is a pretty powerful little statement.
#119250From: jack radiganApr 14, 1988 2:48 PM
Hmmm, seems like strncpy() is a pretty close MID$() function already… -Jack-
#119309From: Vic WagnerApr 14, 1988 10:00 PM
Thanks for pointing that out. I'd forgotten about the use on the left hand side. Because of the dynamic allocation required for the new string, it is almost useful ONLY in interpreted languages.
#119342From: John DraperApr 15, 1988 12:46 AM
Vic, I don't know what Basic Rick was talking about. I've never seen one that would allow MID$ on the left side of an assignment. At any rate, I don't see a problemimplementing it in C if it isn't allowed on the left side. The syntax I've seen has always been as a function, (a true function), and the result is assigned to the lvalue. a$ = mid$(x$,3,4) If a temporary buffer is created to handle the intermediate storage requirements, it would be up to the function to deallocate the memory if necessary. You could probably even use the stack for intermediate storage, and of course the requirement of the trailing zero would be handled by simply plugging one in after the resultant string. -larry
#119461From: Richard Rae/SYSOPApr 15, 1988 7:55 PM
I'm talking about ANY release of MicroSoft BASIC which came out in the last five years or so. Let's see, where do I have a copy here… oh, I know, hold on… <digging Model 100 manual out of closet> This should prove the point, let's see… yep, just as I thought, it's standard. Okay, there are two uses listed: 1) MID$ – Get Middle Characters of String MID$(string expression,position,length) 2) MID$ – Replace Middle Characters of String MIDS(string expression1,position,length)=string expression2 As the Model 100 is pretty old as far as computer ages go, this should indicate that MID$ on the left side of the assignment has been around for quite a while in the MicroSoft product.
#119685From: John DraperApr 17, 1988 2:58 AM
Rick, Trust Microsoft to come out with something like that! It is ugly, non orthoganal (check LEFT$ and RIGHT$, and for that matter, any other functions), and just plain ugly. Oh.. I said that already. No matter.. it bears repeating. "beauty is only skin deep, but ugly goes right to the bone" Think I'll write a few new functions… PEEK(address) = 5 OPEN("filename") = A$ INKEY$(a$) = "Y" Arrrgh! -larry
#119126From: Marlene Zenker/SYSOPApr 13, 1988 8:52 PM
Steve, MID$ reads characters in a string from the specified character position for the amount you tell it to. MID$("READ FROM CHARACTER 6 FOR 4",6,4) returns FROM
#119134From: Doug WingerApr 13, 1988 9:29 PM
Robert, You're going to have to roll your own, as I have never seen it supported. Look into strcat and strcpy, and you might think about using a temporary string to build the new one with. It shouldn't be too hard, as you would index into the original string, then copy the next chars of interest into the temporary string, then copy it to wherever you need it. Hope this helps, Doug