?
Solved

Copy N number of chars from the string...

Posted on 2000-04-08
1
Medium Priority
?
249 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
[X]
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
1 Comment
 
LVL 11

Accepted Solution

by:
mikeblas earned 20 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 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