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;
	
}

>>s = new_cstring;

That is only changing the local copy of the string, not the original. Do not create a new character array, but work directly with the pointer that is in the parameter to that function.

you need to pass the reference to the pointer to make it work. You are just modifying the copy of the pointer here. So the changes will not be visible once you leave the fn.

fn signature could be

void to_lower(const char*& s)

while calling you could do

char s[] = "Test";
const char* p = s;
to_lower(p);
cout << p << endl;

Or those of you that like using std:strings etc.

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

int lowercase ( int c )
{
    return std::tolower ( (unsigned char)c );
}

int main()
{
    std::string foo ( "THIS IS A TEST" );

    std::cout<< foo <<'\n';
    std::transform ( foo.begin(), foo.end(), foo.begin(), lowercase );
    std::cout<< foo <<'\n';
    
    std::cin.get();
}
Comments
Good Advice :)
This question has already been answered. Start a new discussion instead.