Converting text to First Letter Capitalisation

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

dougcl
Premium Member
Premium Member
Posts: 137
Joined: Thu Jun 24, 2010 4:28 pm

Post by dougcl »

reachmexyz wrote:did the code that priyadarshikunal sent worked. Since there is no free statement, the routine would blow up if there are many incoming records. Do a vmstat and the free memory should be coming down drastically depending on the number of records you are processing.
What do you think should be freed?

By the way, I am confused by the malloc statement and I think it's incorrect.

I believe it should be:

Code: Select all

char *result = (char *)malloc (strlen(str)); 
Actually, this is the full expression, but unnecessary because sizeof(char *) is always 1.

Code: Select all

char *result = (char *)malloc (strlen(str) * sizeof(char *)); 
priyadarshikunal
Premium Member
Premium Member
Posts: 1735
Joined: Thu Mar 01, 2007 5:44 am
Location: Troy, MI

Post by priyadarshikunal »

As its just assinging the value to the pointer it should not create any problem.

Even though, it will create problems as the free statement can't be written

need to replace it with new operator

Code: Select all

#include "stdio.h" 
#include "string.h" 
#include "stdlib.h"      
#include "ctype.h" 

char* ConvMCT(char *str)  //Function with string input and string  
{ 
   char *result = new char[sizeof(str)*sizeof(char *)]; 
   int x=0, Flag=1;  // Setting Flag to 1 to make the first letter capital. 
   char CheckStr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

   while(*str) 
   { 
      if(Flag=1)  //Check if the last character was not alphabet. 
      { 
         if(isalpha(*str) and islower(*str)) //Convert to uppercase if its a lower case alphabet. 
         { 
            result[x] = upper(*str); 
         } 
         else 
         { 
            result[x] = *str; //No Change if its already in uppercase or not an alphabet. 
         } 
          
      } 
      else 
      { 
         if(isalpha(*str) and isupper(*str)) 
         { 
            result[x] = tolower(*str); //Convert to lowercase except the first character. 
         } 
         else 
         { 
            result[x] = *str; 
         } 
      } 

      if(!strchr(CheckStr, *str))   //Check if the string is not a-z and A-Z. 
      { 
         Flag=1; 
      } 
      else 
      { 
         Flag =0; 
      } 
      ++x; 
      ++str; 
   } 
   result[x] = '\0'; //Terminate the string 
   return result; //Return the replaced string 
}
can't check the code at the moment but if you have used it and there is some problem, please let me know/post the updated version so that it helps others in future.
Priyadarshi Kunal

Genius may have its limitations, but stupidity is not thus handicapped. :wink:
dougcl
Premium Member
Premium Member
Posts: 137
Joined: Thu Jun 24, 2010 4:28 pm

Post by dougcl »

Are we saying that these are equivalent?

Code: Select all

char *result = new char[sizeof(str)*sizeof(char *)];

Code: Select all

char *result = (char *)malloc (strlen(str) * sizeof(char *)); 
priyadarshikunal
Premium Member
Premium Member
Posts: 1735
Joined: Thu Mar 01, 2007 5:44 am
Location: Troy, MI

Post by priyadarshikunal »

Sizeof gives the byte size and strlen returns the length and hence here it should be fine, new operator will release the memory itself so no memory issue should get created.
Priyadarshi Kunal

Genius may have its limitations, but stupidity is not thus handicapped. :wink:
pbttbis
Premium Member
Premium Member
Posts: 36
Joined: Thu Dec 11, 2014 3:30 am
Location: South Africa
Contact:

Post by pbttbis »

Thanks for the code!!

C++ will be new to many, so just a note for others wanting to use the code above. It needs to be Flag==1 Or Flag>0 otherwise it uppercases the entire string.

Also upper must be toupper.

Also on my compiler I had to change "and" to "&&".

I hope it saves others some time debugging...
PBT TBIS Consultant
Post Reply