Link to home
Start Free TrialLog in
Avatar of TeleKawaru
TeleKawaru

asked on

This is a stupid question.

How do you get a string from the middle of another.

Like if I wanted to pull 40 characters starting from the 4th character of another string? So like how would I easily get characters 4-44?
Avatar of joao_patrao
joao_patrao


You can use a method of CString called mid(start, nºchars)

// example for CString::Mid
CString s( _T("abcdef") );
ASSERT( s.Mid( 2, 3 ) == _T("cde") );

That code is very much MFC specific. Secondly it is a C++ solution. The question is in the C section and a C solution using ANSI C should be provided.

Avatar of TeleKawaru

ASKER

Is there an easier way? I don't know any of those commands.
Actually I use DJGPP and EDIT to code C. ^_^ If that helps.
Many ways

char string="1234567890123456789012345678901234567890";
char dest[41];
char *ptr=string+4
printf ("%s",ptr");
strcpy (dest,ptr);
srncpy (dest,ptr,40); /* if string is longer than 44 chars */
strcpy (dest,(string+4));
strcpy (dest,&(string[4]));

hope this helps.

Almost all of rbr's answers assume that you want the "last" 40 characters of the string. They will work fine if that is the case. If not, and you actually need them from the middle (i.e. the string to be copied from is 80 characters for example) then only the strncpy example is close. You will need to beware of the fact that strncpy does not copy a null character across when the source string is longer than the count argument. The solution (if needed) is to set it yourself (dest[40]='\0';).
Other solutions along those lines are also possible (using memcpy or a loop and pointers), and involve you setting the null yourself as well.

Okay well here's another addition to my question:
I'm just trying to make a scrolling marquee of text in a graphics mode (Allegro) so if someone could tell me how to do it, I will up the points to 400.
my strings are
char greets[256];
char screentext[40];

so how would i continally get a scroll of 40 characters from greets and put it into screentext(the printed text)?
Adjusted points to 400
How about:

int scrollmarquee;
char greets[256];
char screentext[41]; /* note the extra character for the null */
int i;

i=0;
screentext[40]='\0';
while(scrollmarquee)
{   if(i<216)strncpy(screentext,greets+i,40);
    else  /* need end of greets followed by beginning */
    {   strcpy(screentext,greets+i);
        strncpy(screentext+strlen(screentext),greets,40-(256-i));
    }

     /* do display of screentext here */

    if(++i>255)i=0;
}

Avatar of ozo
i=0;
screentext[39]='\0';
while( 1 ){
   memmove(screentext,screentext+1,38);
   screentext[38] = greets[i++];
   i %= 256;
}
There are some boundary condition you may have to fiddle with here, partly since there might be a bug in the sample, partly since I'm not deadcertain of the conditions. For instance does greets contain 255 characters plus a null or (as the screentext spec indicates) 256 characters and you forgot to leave space for the null (in which case it should be declared char greets[257]). Are there actually 256 characters in there, or will the tail of it be a lot of nulls or spaces. In that case you should inspect greets and account for that in the reset condition at the end (if(++i>255)i=0;). scrollmarquee should be set to true, and reset to false somewhere in the middle of the loop if you want to be able to terminate it.

ozo's worked. ozo, please answer the question so I can give you your points.
Well done, ozo
char origBuffer[MAX_BUFFER];
char subBuffer[LEN_SUB_BUFFER];

.
strncpy(subBuffer, origBuffer + 4, 40);
to wnz,
haven't you seen that Telekawaru has already stated that ozo's comment has worked. All he is waiting for is it being changed into an answer. come on wnz - ozo deserves the points, he should get them.
pagladasu
I'm waiting for ozo
Faster solution is to move the screentext pointer, and use ??printf with "%.40s":

const char greets[256];
char *screentext = greets;

while (1) {
    printf("%.40s",screentext);      // or whatever output function you want.
    if ((++screentext) == (greets + 256-40)) screentext = greets;
}

And no memcopy or memmove.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FuzzyLogic, your solution shifts the text to the left, leaving the right blank; it does not scroll around.

 improved in speed, but needs more memory:
i=0;
screentext[39]='\0';
while( 1 ){
        memmove(screentext,screentext+1,38);
        screentext[38] = greets[i++];
        i %= 256;
}
/* Unless you prefer FuzzyLogic's solution, since you didn't mention how screentext was to be used */
It's not that it's not fair, it's just that I wanted more info. It would be not fair if would have denied them the right to answer it as many times as they wanted.
That was strange. I did not add that last comment even though it says I did.
I did not post the above comment!
Ack! This is freaky! I'm not saying this stuff!
What is going on? I'm not adding those comments!
This is very strange.
The repost of my original soution is what I submitted as an answer.
I (ozo) never said anything about the question not being fair.
It looks like the comments have gotten out of sync.
01:48AM PST

We've fixed the attributions of this question.  Please let us know if there are any further problems.
I post this as a comment. Strange that it was stored as an answer for ozo.
the attributions
From: ozo Date: Saturday, November 28 1998 - 01:20AM PST
and
From: ozo Date: Saturday, November 28 1998 - 01:24AM PST
are still incorrect.
I did not make either of those comments