Just use std::string. Otherwise your options are to change the string you are passed or return a dynamically allocated string and remember to dealocate it.
Code:
// with std::string, copying string
std::string sixtofour(std::string str) {
for(int i=0;i<length;++i) {
if(str[i] = '6') str[i] = '4';
}
return str;
}
char str[15] = "25 or 624";
cout << sixtofour(str) << endl;
// prints "25 or 424", str unchanged;
//with char[15]; modifying string
char * sixtofour(char str[15]) {
int length = strlen(str);
for(int i=0;i<length;++i) {
if(str[i] = '6') str[i] = '4';
}
return str;
}
cout << sixtofour(str) << endl;
// prints "25 or 424", str="25 or 424" ;
char *s= sixtofour(str);
s[3] = '2'; // changes str!
char *s2 = sixtofour("25 or 6 to 4");// This will often crash many computers
//with char15, copying string.
char *sixtofour(char str[15]) {
int length = strlen(str);
char * copy = new char[length+1];
copy[0] = '\0';
strcat(copy,str);
for(int i=0;i<length;++i) {
if(copy[i] = '6') copy[i] = '4';
}
return copy;
}
char *s = sixtofour(str);
//str unchanged
s[3] = 2;
//still unchanged
//but you need to do some more
delete [] s;
// clean up after sixtofour() solo