I was trying to format the string of type CString so that when you input for example
once upon a time in Mexico
it would return Once Upon A Time In Mexico

m_VideoName.Replace(m_VideoName[0],toupper(m_VideoName[0]));

for(int i=1; i<m_VideoName.GetLength(); i++)
{
      if(isspace(m_VideoName[i]))
      {
          m_VideoName.Replace(m_VideoName[i+1],toupper(m_VideoName[i+1]));
      }
}

however it keeps returning
Once UpOn A TIMe In MexIcO
Any help would be greatly appreciated

you don't need to use the Replace method -- just simply index into the array

int i = 0;
int len = m_VideoName.GetLength();
while( i < len )
{
    while(i < len && isspace( m_VideoName[i] ) && ispunct(m_VideoName[i]))
       ++i;
    if( i < len )
        m_VideoName[i] = toupper(m_VideoName[i]);
}

The reason I used the replace method was that when I use

m_VideoName[i] = toupper(m_VideoName[i]);

I get an error '=' : left operand must be l-value

Replace() is the wrong method to use because you can't control what characters that get replaced in what positions. Here is another solution that I tested and works

CString m_VideoName = "hello world";
	int len = m_VideoName.GetLength();
	int i = 0;
	while( i < len )
	{
		// advance to the first non-space character
		while(i < len && (isspace( m_VideoName[i] ) || ispunct(m_VideoName[i])))
			++i;
		if( i < len )
		{
			// convert the character to upper case
			CString str = m_VideoName.Mid(0,i);
			str += toupper(m_VideoName[i]);
			str += m_VideoName.Mid(i+1);
			m_VideoName = str;
		}
		// advance to the beginning of the next white space character
		while(i < len && !isspace( m_VideoName[i] ) )
			++i;

	}

So this is what i went with in the end
i used AppendChar because i kept getting an error when i tried

m_VideoName += toupper(m_VideoName[i]);

It wasn't letting me use the toupper with the +=

CString tempString;
	int len = m_VideoName.GetLength();

	tempString.AppendChar((toupper(m_VideoName[0])));

	for(int i=1; i<len; i++)
	{
		if(isspace(m_VideoName[i-1]))
			tempString.AppendChar(toupper(m_VideoName[i]));
		

		else if(isspace(m_VideoName[i-1]) && i<len)
			continue;

		else
			tempString.AppendChar(m_VideoName[i]);
	}
	m_VideoName = tempString;

Thanks for all the help Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.