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.
#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.
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.
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 "&&".