Solved

Copy N number of chars from the string...

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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