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

Recommended Answers

All 5 Replies

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;

	}

Thanks for the help that's way better than my way

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.