[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

This is a stupid question.

Posted on 1998-11-26
29
Medium Priority
?
206 Views
Last Modified: 2010-05-18
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
Comment
Question by:TeleKawaru
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
  • 3
  • +6
29 Comments
 
LVL 3

Expert Comment

by:joao_patrao
ID: 1254639

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
 
LVL 4

Expert Comment

by:pagladasu
ID: 1254640
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
 

Author Comment

by:TeleKawaru
ID: 1254641
Is there an easier way? I don't know any of those commands.
0
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!

 

Author Comment

by:TeleKawaru
ID: 1254642
Actually I use DJGPP and EDIT to code C. ^_^ If that helps.
0
 
LVL 10

Expert Comment

by:rbr
ID: 1254643
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
 
LVL 16

Expert Comment

by:imladris
ID: 1254644
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
 

Author Comment

by:TeleKawaru
ID: 1254645
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
 

Author Comment

by:TeleKawaru
ID: 1254646
Adjusted points to 400
0
 
LVL 16

Expert Comment

by:imladris
ID: 1254647
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
 
LVL 84

Expert Comment

by:ozo
ID: 1254648
i=0;
screentext[39]='\0';
while( 1 ){
   memmove(screentext,screentext+1,38);
   screentext[38] = greets[i++];
   i %= 256;
}
0
 
LVL 16

Expert Comment

by:imladris
ID: 1254649
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
 

Author Comment

by:TeleKawaru
ID: 1254650
ozo's worked. ozo, please answer the question so I can give you your points.
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1254651
Well done, ozo
0
 

Expert Comment

by:wnz
ID: 1254652
char origBuffer[MAX_BUFFER];
char subBuffer[LEN_SUB_BUFFER];

.
strncpy(subBuffer, origBuffer + 4, 40);
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1254653
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
 

Author Comment

by:TeleKawaru
ID: 1254654
I'm waiting for ozo
0
 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1254655
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
 
LVL 84

Accepted Solution

by:
ozo earned 1600 total points
ID: 1254656
It's not fair to alter the question aftzer one was answered. Post a new question.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254657
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
 

Author Comment

by:TeleKawaru
ID: 1254658
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
 

Author Comment

by:TeleKawaru
ID: 1254659
That was strange. I did not add that last comment even though it says I did.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254660
I did not post the above comment!
0
 

Author Comment

by:TeleKawaru
ID: 1254661
Ack! This is freaky! I'm not saying this stuff!
0
 

Author Comment

by:TeleKawaru
ID: 1254662
What is going on? I'm not adding those comments!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254663

0
 
LVL 84

Expert Comment

by:ozo
ID: 1254664
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
 
LVL 1

Expert Comment

by:wormzer
ID: 1254665
We've fixed the attributions of this question.  Please let us know if there are any further problems.
0
 
LVL 10

Expert Comment

by:rbr
ID: 1254666
I post this as a comment. Strange that it was stored as an answer for ozo.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1254667
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question