Hi,
I'm quite new to C++ and I'm trying to write a function which would change uppercase letter in char[] to lowercase. Inside this function everything is fine but when I leave it, contents of char[] is not changed. Any ideas why? Also when I try to delete local pointers I'm getting error. Thanks for any help with this.
void to_lower(const char* s)
{
if (!s) return; //if s doesn't point to anything
int size = 5;
char* new_cstring = new char[size];
for (int i = 0; i < size; new_cstring[i] = 0, ++i); //initialize array with 0
int counter = 0;
//checks if there are still characters in the array
while (*s)
{
//if it's an UPPERCASE letter
if((int)*s >= 65 && (int)*s <= 90)
{
new_cstring[counter] = ((int)*s + 32); //changes this letter to lowercase
}
else
{
new_cstring[counter] = *s;
}
++counter;
++s;
if (counter == size - 1)
{
//creating new array to store contents of new_cstring
char* arr = new char[counter];
for (int i = 0; i < counter; arr[i] = 0, ++i); //initializes arr with 0
int i = counter;
for (; i >= 0; arr[i] = new_cstring[i], --i); //copies from old array to new one
new_cstring = new char[counter + 10]; //creates new bigger array
for(int i = 0; i < (counter + 10); new_cstring[i] = 0, ++i); //initialize new_string with 0
for (int i = 0; i < counter; new_cstring[i] = arr[i], ++i);//copies from arr to new_cstring
size = counter + 10;
//delete[] arr;
}
}
s = new_cstring;
}