Access violation writing location. Exception thrown at 0x52CEE39 (uncrtbased.dll).

This is a replace function like MS Word. sentence is the original text. ind is the starting indexes of the word which the user wants to replace. (I have already found the indexes of the word and stored them in ind), wlength is the length of the word which is going to be replaced. Temp is where I store my result. nword is the new word that is going to be in place of the old word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  void replace(char sentence[], char ind[], int wlength, char temp[], char nword[]) {

int a = 0, x = 0, t=0, j = 0;
for (int i = 0; i < stringlength(ind); i++)
{
while (a < ind[i])
{
temp[t] sentence[a];
t++;
a++;
}
a = a + wlength;
t = t + 1;
strcat (temp, nword); // <---- Error
}
for (int x 0; x<< stringlength(temp), x
cout<< tempt;}
That's obviously not your actual code since it's got a bunch of syntax errors in it.
Anyway, the strcat might be blowing up because you haven't zero-terminated temp before calling it.
You don't need to use strcat since you know exactly where you want to copy nword.
You could use strcpy with an offset, like strcpy(temp + t, nword). (Or I guess it's temp + t - 1 since you've incremented t.)

Topic archived. No new replies allowed.