• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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?
0
TeleKawaru
Asked:
TeleKawaru
  • 10
  • 7
  • 3
  • +6
1 Solution
 
joao_patraoCommented:

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") );

0
 
pagladasuCommented:
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.

0
 
TeleKawaruAuthor Commented:
Is there an easier way? I don't know any of those commands.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
TeleKawaruAuthor Commented:
Actually I use DJGPP and EDIT to code C. ^_^ If that helps.
0
 
rbrCommented:
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.

0
 
imladrisCommented:
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.

0
 
TeleKawaruAuthor Commented:
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)?
0
 
TeleKawaruAuthor Commented:
Adjusted points to 400
0
 
imladrisCommented:
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;
}

0
 
ozoCommented:
i=0;
screentext[39]='\0';
while( 1 ){
   memmove(screentext,screentext+1,38);
   screentext[38] = greets[i++];
   i %= 256;
}
0
 
imladrisCommented:
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.

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

.
strncpy(subBuffer, origBuffer + 4, 40);
0
 
pagladasuCommented:
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
0
 
TeleKawaruAuthor Commented:
I'm waiting for ozo
0
 
FuzzyLogicCommented:
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.
0
 
ozoCommented:
It's not fair to alter the question aftzer one was answered. Post a new question.
0
 
ozoCommented:
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 */
0
 
TeleKawaruAuthor Commented:
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.
0
 
TeleKawaruAuthor Commented:
That was strange. I did not add that last comment even though it says I did.
0
 
ozoCommented:
I did not post the above comment!
0
 
TeleKawaruAuthor Commented:
Ack! This is freaky! I'm not saying this stuff!
0
 
TeleKawaruAuthor Commented:
What is going on? I'm not adding those comments!
0
 
ozoCommented:

0
 
ozoCommented:
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

0
 
wormzerCommented:
We've fixed the attributions of this question.  Please let us know if there are any further problems.
0
 
rbrCommented:
I post this as a comment. Strange that it was stored as an answer for ozo.
0
 
ozoCommented:
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 7
  • 3
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now