Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CString to an array of chars

Status
Not open for further replies.

Pyropat

Programmer
May 21, 2001
9
0
0
US
I need to take a word that the user inputed from an edit box and divide it into it's individual letters.
How do I do this?
 
A couple of ways:

1) if you want the CString in a char array, just use sprintf -

char m_cChar[7];
CString m_sString;

m_sString.Format("TESTING");

sprintf(m_cChar, "%s", m_sString);

Then m_cChar[0] = "T", m_cChar[1] = "E", etc.

2) you can use GetAt if you just want to use the CString you already have -

CString m_sString;

m_sString.Format("TESTING");

Then m_sString.GetAt(0) = "T", m_sString.GetAt(1) = "E", etc

Ed
 
Assume you know how to get the text from the edit box.

Actually, you can read individual character from the CString object using the overloaded [] operator:
[tt][ignore]
CString strText
// Read edit box data
int iLen = strText.GetLength();
for (int i = 0; i < iLen i++)
{
char ch = strText;
...
}

[/ignore][/tt]
You can store the character to a CArray object or a STL vector object.
 
Didn't know CString had the [] operator, I just learned something. Obviously the easiest way to go. Thanks sflam.

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top