Solved

Copy N number of chars from the string...

Posted on 2000-04-08
1
243 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
Comment Utility
> 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

19 Experts available now in Live!

Get 1:1 Help Now