Why calling unAnnoyWord(char *str)
with the following input: "hakuna matata" returns:
Exception EXC_BAD_ACCESS (code=2, address=0x10dbc9fa8)
#define RANGE 128
char annoying(char *str)
{
char occurences[RANGE] = { 0 }; char mostAnnoying = '\0';
do {
if (++occurences[*str] > occurences[mostAnnoying])
mostAnnoying = *str;
}
while (*str++);
return mostAnnoying;
}
void unAnnoyWord(char *str) {
int i=0;
char *out = malloc(sizeof(char)*(strlen(str)+1)), mostAnnoying = annoying(str);
do
{
if (*str != mostAnnoying)
{
out[i++] = *str;
}
} while (*str++);
out[i]='\0';
strcpy(str,out);
}
After debugging my code I can confirm that this line causes the issue:
strcpy(str,out);
in main() I have:
char* str="hakuna matata";
unAnnoyWord(str);
Edited version:
int i=0,j=0;
char *out = malloc(sizeof(char)*(strlen(str)+1)), mostAnnoying = annoying(str);
do
{
if (str[j] != mostAnnoying)
{
out[i++] = str[j];
}
} while (str[j++]);
out[i]='\0';
strcpy(str,out);
Why calling unAnnoyWord(char *str)
with the following input: "hakuna matata" returns:
Exception EXC_BAD_ACCESS (code=2, address=0x10dbc9fa8)
#define RANGE 128
char annoying(char *str)
{
char occurences[RANGE] = { 0 }; char mostAnnoying = '\0';
do {
if (++occurences[*str] > occurences[mostAnnoying])
mostAnnoying = *str;
}
while (*str++);
return mostAnnoying;
}
void unAnnoyWord(char *str) {
int i=0;
char *out = malloc(sizeof(char)*(strlen(str)+1)), mostAnnoying = annoying(str);
do
{
if (*str != mostAnnoying)
{
out[i++] = *str;
}
} while (*str++);
out[i]='\0';
strcpy(str,out);
}
After debugging my code I can confirm that this line causes the issue:
strcpy(str,out);
in main() I have:
char* str="hakuna matata";
unAnnoyWord(str);
Edited version:
int i=0,j=0;
char *out = malloc(sizeof(char)*(strlen(str)+1)), mostAnnoying = annoying(str);
do
{
if (str[j] != mostAnnoying)
{
out[i++] = str[j];
}
} while (str[j++]);
out[i]='\0';
strcpy(str,out);
Why calling unAnnoyWord(char *str)
with the following input: "hakuna matata" returns:
Exception EXC_BAD_ACCESS (code=2, address=0x10dbc9fa8)
#define RANGE 128
char annoying(char *str)
{
char occurences[RANGE] = { 0 }; char mostAnnoying = '\0';
do {
if (++occurences[*str] > occurences[mostAnnoying])
mostAnnoying = *str;
}
while (*str++);
return mostAnnoying;
}
void unAnnoyWord(char *str) {
int i=0;
char *out = malloc(sizeof(char)*(strlen(str)+1)), mostAnnoying = annoying(str);
do
{
if (*str != mostAnnoying)
{
out[i++] = *str;
}
} while (*str++);
out[i]='\0';
strcpy(str,out);
}
After debugging my code I can confirm that this line causes the issue:
strcpy(str,out);
in main() I have:
char* str="hakuna matata";
unAnnoyWord(str);
Edited version:
int i=0,j=0;
char *out = malloc(sizeof(char)*(strlen(str)+1)), mostAnnoying = annoying(str);
do
{
if (str[j] != mostAnnoying)
{
out[i++] = str[j];
}
} while (str[j++]);
out[i]='\0';
strcpy(str,out);
Why calling unAnnoyWord(char *str)
with the following input: "hakuna matata" returns:
Exception EXC_BAD_ACCESS (code=2, address=0x10dbc9fa8)
#define RANGE 128
char annoying(char *str)
{
char occurences[RANGE] = { 0 }; char mostAnnoying = '\0';
do {
if (++occurences[*str] > occurences[mostAnnoying])
mostAnnoying = *str;
}
while (*str++);
return mostAnnoying;
}
void unAnnoyWord(char *str) {
int i=0;
char *out = malloc(sizeof(char)*(strlen(str)+1)), mostAnnoying = annoying(str);
do
{
if (*str != mostAnnoying)
{
out[i++] = *str;
}
} while (*str++);
out[i]='\0';
strcpy(str,out);
}
After debugging my code I can confirm that this line causes the issue:
strcpy(str,out);
in main() I have:
char* str="hakuna matata";
unAnnoyWord(str);
Edited version:
int i=0,j=0;
char *out = malloc(sizeof(char)*(strlen(str)+1)), mostAnnoying = annoying(str);
do
{
if (str[j] != mostAnnoying)
{
out[i++] = str[j];
}
} while (str[j++]);
out[i]='\0';
strcpy(str,out);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.