LowerCase pointer Function

Hi!, I'm trying to make a lowercase function, the problem here is that, I don't know how can I assign a char array to a pointer, I'm using calloc to alloc the memory for my pointer. Do I need another function to fill that memory ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "stdafx.h"
#include <cstdlib>
#include <iostream>

void Lower(char *t);



int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	char text[] = "Anita";
	char *ptext;
	ptext = (char*)calloc(strlen(text), sizeof(char));
	

	
	cout<<ptext<<endl;
	cout<<text<<endl;


	delete ptext;
	system("PAUSE");
	return 0;
}


void Lower(char *t)
{
	char up[]    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char low[]   = "abcdefghijklmnopqrstuvwxyz";
	char *pup  = up;
	char *plow = low;
	

	for (int i=0; i<strlen(t); i++)
	{

		for (int j=0; j<strlen(up); j++)
		{
			if(t[i]==pup[j])
			{
				t[i]=plow[j];
			}

		}

	}
	
}




the "Lower" function is not complete, and filled with errors btw, but I'm sure you'll catch the idea.

Any suggestions?
Last edited on
*ptext = text;

This doesn't make any sense. ptext is a pointer to a char, so *ptext is a char. Is text a char? No. So how can you assign the value of text to a char? What are you actually trying to do with that? Are you trying to copy the contents of the array that text points to into the memory you just allocated?

Is there a reason you're doing this using an array of char instead of just using a proper C++ string?
Last edited on
We can't assign the value of text to a char, because it is an array, I've just forgot to change that u_U.

the reason here is that I'm trying to optimize a code that looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void ToLower(char *t)
{
for(int i=0; i<strlen(t); i++)
{
if(t[i]=='A')
t[i]='a';
if(t[i]=='B')
t[i]='b';
.....


if(t[i]=='Z')
t[i]='z';

}
}


I don't want any kind of memory leak. that's all.

I'm editing the post, so it does not contain *ptext = text;

I'm having a question, if I'm using calloc or malloc, should I use "delete"? or it only works when using "new"?
Last edited on
You shouldn't be using calloc or malloc. You should be using new. But actually, you shouldn't be using new, you should be using string, and the algorithm library.

1
2
std::string sampleString= "sOmetexT"; 
std::transform(sampleString.begin(), sampleString.end(), sampleString.begin(), ::tolower);

Last edited on
I know I can use tolower functions, or transform, but the case is that this code should work with pointers, also, it has to be a lowercase function. Pointers are very complex...
Last edited on
Pointers are very complex...


Pointers are very simple. Really, really simple.
http://www.cplusplus.com/articles/EN3hAqkS/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <cstring>

int main()
{
char text[] = "Anita";
char *ptext = text;

while (*ptext)
{
  *ptext = (char)tolower(*ptext);
  ptext++;
  
}

std::cout << text;



}

Hi!, I did solve this code, without the use of tolower functions, I'm just using the Ascii Table,

could you tell me if this code is all right?

any improvements?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "stdafx.h"
#include <cstdlib>
#include <iostream>

void Lower(char *t);

void sort(unsigned char *v, int len);




int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	char text[] = "ANITA";
	char *ptext = text;
	
	Lower(ptext);
	

	
	cout<<ptext<<endl;
	
	ptext = 0;
	system("PAUSE");
	return 0;
}



void Lower(char *t)
{
	int asciiTmp;
	

	for(int i = 0 ; i<strlen(t); i++)
	{
		asciiTmp=(int)t[i];

		if(asciiTmp>=65 && asciiTmp<=90)
		{
			asciiTmp=asciiTmp+32;
			t[i] = asciiTmp;
			
		}


	}

}


Thanks for your help!!!
Last edited on
Topic archived. No new replies allowed.