Solved

This is a stupid question.

Posted on 1998-11-26
29
199 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
  • 10
  • 7
  • 3
  • +6
29 Comments
 
LVL 3

Expert Comment

by:joao_patrao
Comment Utility

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
Comment Utility
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
Comment Utility
Is there an easier way? I don't know any of those commands.
0
 

Author Comment

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

Expert Comment

by:rbr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points to 400
0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ozo's worked. ozo, please answer the question so I can give you your points.
0
 
LVL 4

Expert Comment

by:pagladasu
Comment Utility
Well done, ozo
0
 

Expert Comment

by:wnz
Comment Utility
char origBuffer[MAX_BUFFER];
char subBuffer[LEN_SUB_BUFFER];

.
strncpy(subBuffer, origBuffer + 4, 40);
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:pagladasu
Comment Utility
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
Comment Utility
I'm waiting for ozo
0
 
LVL 1

Expert Comment

by:FuzzyLogic
Comment Utility
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 400 total points
Comment Utility
It's not fair to alter the question aftzer one was answered. Post a new question.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
That was strange. I did not add that last comment even though it says I did.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I did not post the above comment!
0
 

Author Comment

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

Author Comment

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

Expert Comment

by:ozo
Comment Utility

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
I post this as a comment. Strange that it was stored as an answer for ozo.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now