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
Solved

Copy N number of chars from the string...

Posted on 2000-04-08
1
246 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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