• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 927
  • Last Modified:

substring for char*

Hi

I have a char* variable

I want to use substring.

I want to know if there is any substring function for char*

Regards
Karan
0
KaranGupta
Asked:
KaranGupta
  • 5
  • 2
2 Solutions
 
AndyAinscowCommented:
   char s[] = "please help me";
    char s2[5];
    ZeroMemory(s2, 5);
    _tcsncpy(s2, &s[7], 4);


s2 now contains "help"
0
 
AndyAinscowCommented:
Or did you want strstr -
Returns a pointer to the first occurrence of a search string in a string.

copied from help files:

// crt_strstr.c

#include <string.h>
#include <stdio.h>

char str[] =    "lazy";
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] =   "         1         2         3         4         5";
char fmt2[] =   "12345678901234567890123456789012345678901234567890";

int main( void )
{
   char *pdest;
   int  result;
   printf( "String to be searched:\n   %s\n", string );
   printf( "   %s\n   %s\n\n", fmt1, fmt2 );
   pdest = strstr( string, str );
   result = (int)(pdest - string + 1);
   if ( pdest != NULL )
      printf( "%s found at position %d\n", str, result );
   else
      printf( "%s not found\n", str );
}
0
 
KaranGuptaAuthor Commented:
Hi Andy

I have tried your first solution

I am getting an error

C2664: 'wcsncpy' : cannot convert parameter 1 from 'char [5]' to 'wchar_t *'      c:\Documents and
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
AndyAinscowCommented:
Those functions are for char (as you asked for) not for wchar.
Try replacing char with TCHAR in the example I gave (that reverts to char for ASCII and wchar for UNICODE)
0
 
AndyAinscowCommented:
Alternatively, if you are working with char strings in a UNICODE app then replace _tcsncpy with strncpy.
0
 
KaranGuptaAuthor Commented:
Hi Andy

I have tried this code what you gave me in your first post

char s[] = "please help me";
char s2[5];
ZeroMemory(s2, 5);
_tcsncpy(s2, &s[7], 4);

I haven't try anything different

Regards
Karan

0
 
AndyAinscowCommented:
char s[] = "please help me";
char s2[5];
ZeroMemory(s2, 5);
strncpy(s2, &s[7], 4);


OR

TCHAR s[] = "please help me";
TCHAR s2[5];
ZeroMemory(s2, sizeof(s2));
_tcsncpy(s2, &s[7], 4);
0
 
itsmeandnobodyelseCommented:
>>>> TCHAR s[] = "please help me";

If it doesn't compile do

TCHAR s[] = _T("please help me");


FYI:
The TCHAR  and _T is a means (mess) Microsoft provided to be able to switch from single char to wide char (UNICODE) and back by simple defining a preprocessor macro. In your project the macro _UNICODE was set, hence TCHAR switches to wchar_t and hence the errors if using 'char' type.

If you *know* that your string is char string you should not bother with widestrings but do:

char sz[] = "please help me";
char ssz[sizeof(sz)] = { '\0' };   // makes all chars zero
strncpy(ssz, sz + 7, 4);              // copies help to ssz which is zero-terminated

If your string already is in CString do:

CString  s = "please help me";
CString  sub = s.Mid(7, 4);  

If not I would recommend using std::string class:

#include <string>
using namespace std;

string  s = "please help me";
string  sub = s.substr(7, 4);

If you need to pass the sub as (const) char* to a function do

   funcTakingAConstCharPtr(sub.c_str());

 



0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now