+ Reply to Thread
Results 1 to 2 of 2

Thread: How can I make string upper/lower in single statement?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    23

    How can I make string upper/lower in single statement?

    Hi all,

    Currently, I am using following code to convert string to lower or upper case.

    How to make it smaller in code and more readable.

    Code:
    void ConvertString()
    {
    	char szData[] = "Hello World" ;
    
    	char szOutput[100] ;
    
    	memset(szOutput, 0, sizeof(szOutput)) ;
    	for (int n=0; n<strlen(szData); n++)
    	{
    		szOutput[n] = tolower(szData[n]) ;
    	}
    
    	memset(szOutput, 0, sizeof(szOutput)) ;
    	for (n=0; n<strlen(szData); n++)
    	{
    		szOutput[n] = toupper(szData[n]) ;
    	}
    }

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Use std::transform as follows:

    Code:
    void ConvertString()
    {
    	char szData[] = "Hello World" ;
    
    	char szOutput[100] = "" ;
    
    	std::transform(&szData[0], &szData[strlen(szData)], &szOutput[0], tolower) ;
    
    	std::transform(&szData[0], &szData[strlen(szData)], &szOutput[0], toupper) ;
    }
    Thanks.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts