We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

substring for char*

Medium Priority
951 Views
Last Modified: 2013-12-14
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
Comment
Watch Question

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

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


s2 now contains "help"
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
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 );
}

Author

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
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
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)
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Alternatively, if you are working with char strings in a UNICODE app then replace _tcsncpy with strncpy.

Author

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

Freelance programmer / Consultant
CERTIFIED EXPERT
Commented:
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);

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
>>>> 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());

 



Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.