Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

displaying middle part of strings.

Status
Not open for further replies.

AndyDoyle

Programmer
Jan 31, 2003
5
US
Just a quickie,

I have some various length strings, which are all '\n' terminated.
However I need to display them broken up into different parts.

eg if one is 20 chars and I need to break up into 4 chars then 12 chars then 4 chars.

cvhh cjhbsdhcjugh bhsy

Thankyou.
Sorry its not more technical.
 
You just need a strncpy wrapper probably.

Code:
char *wrapIt(char *target, int offset, int len) {
char *place, *new;

   /*make sure the options are sane*/
   if (len <= (strlen(target) - offset)) {
       /*move to beginning of selection*/
       place = target + offset;
       /*allocate memory for new string*/
       new = malloc(len + 1 * sizeof(char));

            if (new) {
            /* if we've got mem cut string */
               strncpy(new,place,len);
            /* terminate the string*/
               new[len + 1] = '\0';
               return new;
            }  
    }
/* we can check for a NULL return now and know
that an error occurred, more specific errors
could be diagnosed for invalid args or memalloc
failure*/
return NULL;
}

Usage:
char *pstr;
pstr = wrapIt(&quot;This is a bogus string&quot;,5,3);
printf(&quot;%s\n&quot;, pstr);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top