Solved

Copy N number of chars from the string...

Posted on 2000-04-08
1
244 Views
Last Modified: 2013-11-20
Hi
I got this problem i need to copy a substring from position i set...
something like that..

char * copy(char *str,int pos,int n);

str-oridinal string
pos- position of the char to start,
n-number of chars to copy from that pos.
and the function returns the subtring//

is there such function in Visual C++?

if there is pls tell a\meabout it.
0
Comment
Question by:yans
1 Comment
 
LVL 11

Accepted Solution

by:
mikeblas earned 5 total points
ID: 2696435
> is there such function in Visual C++?

There is no such function built-in.  It's easy to write, though.

Note that I coded your function to allocate a new string and then copy the substring into the newly allocated memory. I suppose you could modify the original string in place, but that's not what I chose to do.

For only five points, you get no choice in the matter.

--- begin file substr.cpp ---

// compile with:
//     cl /W4 substr.cpp

#include <windows.h>
#include <stdio.h>

char* copy(const char* pstr, int nPos, int nLen)
{
   if (nPos < 0 || pstr == NULL)
      return NULL;

   char *pstrReturn;

   if (nPos > lstrlen(pstr))
   {
      pstrReturn = new char[1];
      *pstrReturn = 0;
   }
   else
   {
      pstrReturn = new char[nLen + 1];
      lstrcpyn(pstrReturn, pstr + nPos, nLen + 1);
   }

   return pstrReturn;
}

void main()
{
   char* pstrSubString = copy("Hockey is just like life", 10, 9);

   printf("substring is \"%s\"\n", pstrSubString);

   // remember to return the string we allocated!
   delete [] pstrSubString;

   return;
}

--- end file substr.cpp ---

..B ekiM
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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