Need BigTime Help.

I am having a great deal of trouble with strings and chars in C++. Judging by the posts at this site, I see I'm not alone. Other programming languages give me no problem -- I can manipulate strings pretty well. Here is my problem.

How can output a string type variable into a windows rectangle? DrawText(HDC,LPCTSTR,int,LPRECT,UINT) expects a char pointer as LPCTSTR but the data is declared as a string type. I need the data as a string type because I need to extract it from a string using "string.find". Can I convert the extracted string to char? If not, how do extract a substring from char. In other words, does there exist, or can I create a similar function to do the same thing with char as "string.find" does with strings.

Let me try to make this clear:
string mystring="123456789" mystring.find("3",0) returns position 4.
char *mychar="123456789" how do I find the position of "3"?


10 Years
Discussion Span
Last Post by GloriousEremite

Use string::c_str(). It gives a constant C style character string. Use this when a datatype of LPCTSTR is needed.

Now your second question does not need an answer.

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
   string mystring="123456789";
   std::cout << mystring.find("3",0) << '\n';
   char *mychar="123456789"; // how do I find the position of "3"?
   char *found = strstr(mychar, "3");
   std::cout << found - mychar << '\n';
   return 0;

/* my output

IF after Wolfpacks answer u still need to know how to find the position of any char in a char* array of chars then maybe this is wat u are looking for:

size_t strcspn( const char *str1, const char *str2 );

The function strcspn() returns the index of the first character in str1 that matches any of the characters in str2.

SO maybe

char* delimiter = "3";
char* myString = "1234567689";
int position = strcspn (myString, delimiter);

Hope it helped, bye.


Of course, if you're compiling with UNICODE defined, you'll have problems. You could probably do something like:

namespace MyProg
typedef std::basic_string<TCHAR> string;
MyProg::string str(TEXT("Hello world"));

This would compile with or without UNICODE defined.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.