D
dbuser
Dear experts:
Hope you can point my error here. This function of mine receives an
array of length 32 and then the for loop adds some scrambled characters
at patterns. I think, I can not read the array w[] beyond 32 , but dont
know how to achieve the result. Thanks a lot for help !
void scramble( char *w, int key)
{
x = key; // key = (0-9)
int len = strlen(w);
cout << "length is:" << len << endl; // 32
len = (len + len/x); // 48
cout << "length is:" << len << endl;
char s[] = "@#$"; //array to hold scramble characters
int n = strlen(s);
int i,j,k,m=0;
char * c;
c = new char[len+1];
memset(c, 0, sizeof(c));
//if (c == NULL) exit(1);
for (i=0,j=0,k=0; i<len-1; i++){
if(k<x) {
c[j++] = w;k++;
}
else {
if(m<n)
c[j++] = s[m++];
else {
c[j++] = s[0]; m = 1;}
c[j++]=w; k=1; }
}
cout << "scrambled output is :" << c << endl;
delete c;
}
The output is this, which is desired but it has run time error as
mentioned.
input is : ba ced fhgi kzl nmpo qsr tvuxwzy
scrambled output is :ba@ c#ed$ f@hg#i $kz@l #nm$po@ q#sr$ t@vu#xw$zy@
Hope you can point my error here. This function of mine receives an
array of length 32 and then the for loop adds some scrambled characters
at patterns. I think, I can not read the array w[] beyond 32 , but dont
know how to achieve the result. Thanks a lot for help !
void scramble( char *w, int key)
{
x = key; // key = (0-9)
int len = strlen(w);
cout << "length is:" << len << endl; // 32
len = (len + len/x); // 48
cout << "length is:" << len << endl;
char s[] = "@#$"; //array to hold scramble characters
int n = strlen(s);
int i,j,k,m=0;
char * c;
c = new char[len+1];
memset(c, 0, sizeof(c));
//if (c == NULL) exit(1);
for (i=0,j=0,k=0; i<len-1; i++){
if(k<x) {
c[j++] = w;k++;
}
else {
if(m<n)
c[j++] = s[m++];
else {
c[j++] = s[0]; m = 1;}
c[j++]=w; k=1; }
}
cout << "scrambled output is :" << c << endl;
delete c;
}
The output is this, which is desired but it has run time error as
mentioned.
input is : ba ced fhgi kzl nmpo qsr tvuxwzy
scrambled output is :ba@ c#ed$ f@hg#i $kz@l #nm$po@ q#sr$ t@vu#xw$zy@