c string-problem
tnavid (3)
Jan 24, 2013 at 10:09pm UTC
hi, i want to convert my strings to uppercase strings.but my function does not work.
note:i don't want to use string.h library.
here is my code:
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
#include <Windows.h>
#include <iostream>
using namespace std;
char * cct(char []);
void main()
{
char *bpl[] = {"cAr" ,"Pen" , "hOmE" , "coMputeR" };
cout<<cct(bpl[0])<<endl;
cout<<cct(bpl[1])<<endl;
cout<<cct(bpl[2])<<endl;
cout<<cct(bpl[3])<<endl;
}
char * cct(char k[])
{
char m[10];
for (int i=0;i<=(strlen(k)-1);i++)
{
m[i]=toupper((int )k[i]);
}
return m;
}
coder777 (2549)
Jan 25, 2013 at 8:22am UTC
m is a local variable that becomes invalid as soon as the function cct is left.
So either you allow k to be modified or you pass another buffer that takes the changes (and may be returned)
ajh32 (163)
Jan 25, 2013 at 8:45am UTC
m is a local variable that becomes invalid as soon as the function cct is left.
There are so many questions on this forum on exactly the same issue, trying to return local variable value back from function!
FastLearner (4)
Jan 26, 2013 at 8:32am UTC
I think this should work. I have used dynamic memory allocation. For the first version I do not delete but in the second I do:
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
//First version
#include <Windows.h>
#include <iostream>
using namespace std;
char * cct(char []);
void main()
{
char * bpl[] = {"cAr" ,"Pen" , "hOmE" , "coMputeR" };
cout << cct(bpl[0]) << endl;
cout << cct(bpl[1]) << endl;
cout << cct(bpl[2]) << endl;
cout << cct(bpl[3]) << endl;
}
char * cct(char k[])
{
char *m = new char [10];
int i;
for ( i = 0; i < strlen(k); i++)
{
m[i] = toupper(int (k[i]));
}
m[i] = '\0' ;
return m;
}
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
//Second version
#include <Windows.h>
#include <iostream>
using namespace std;
char * cct(char []);
void main()
{
char * bpl[] = {"cAr" ,"Pen" , "hOmE" , "coMputeR" };
char * m = cct(bpl[0]);
cout << m << endl;
delete m;
m = cct(bpl[1]);
cout << m << endl;
delete m;
m = cct(bpl[2]);
cout << m << endl;
delete m;
m = cct(bpl[3]);
cout << m << endl;
delete m;
}
char * cct(char k[])
{
char *m = new char [10];
int i = 0;
for (i = 0; i < strlen(k); i++)
{
m[i] = toupper(int (k[i]));
}
m[i] = '\0' ;
return m;
}
But what I don't understand is why in line 25 this produces an error, why can't we use k again on the left-hand side:
k[i] = toupper(int (k[i]))
FastLearner (4)
Jan 26, 2013 at 8:52am UTC
It crashes if you define it as:
1 2 3 4 5 6 7 8 9 10 11 12
char * cct(char k[])
{
//char *m = new char[10];
int i = 0;
for (i = 0; i < strlen(k); i++)
{
k[i] = toupper(int (k[i]));
}
k[i] = '\0' ;
return k;
}
This crashes, that's why I need m
ne555 (4383)
Jan 27, 2013 at 12:49am UTC
> trying to return local variable value back from function!
Nothing wrong with that. The problem is to return an address/reference to a local variable.
@FastLearner:const char * bpl[] = {"cAr" ,"Pen" , "hOmE" , "coMputeR" };
And use delete [] if you allocated with new []
@OP:
> i don't want to use string.h library.
You are using it, for `strlen()'
Also, main must return int
FastLearner (4)
Jan 27, 2013 at 1:53am UTC
Thanks ne555. I had forgotten that I cannot assign a new value to a constant. Since it is an array of string literals, it produces an error. Also thanks for noting my mistake with new [] and delete []