Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting memory fault message

Status
Not open for further replies.

anatazi

Programmer
Jun 24, 2002
33
US
Hello all,
I wrote this program that replace a string with another, using my own varaition on string function and now can not get it to work.Please take a look and tell me what I am doing wrong.

I have pasted below the code for all the functions I am using. Thanks in advance.

#include<stdio.h>
#include<string.h>
#define CHARS 30

void main(void)
{
//declare functions

int stringlength (char str[]);
void insertstring (char source[], char str2[], int n);
int findstring (char* s1, char* s2);
void removestring (char *s, int i, int n);
void replacestring (char source[],char str1[], char str2[]);

//declare variable

char text[CHARS];

//copy string to char array text

strcpy(text,&quot; 1* for all and all for 1*&quot;);


replacestring(text, &quot;1&quot;, &quot;one&quot;);
replacestring(text, &quot;*&quot;, &quot;&quot;);
printf(&quot;%s\n&quot;, text);
}
int findstring(char *s1,char *s2)
{
char *p1=s1,*p2=s2,*tmp;

do {
while ((*p1 != *p2)&&(*p1 != '\0')) p1++;
tmp = p1;
while ((*tmp == *p2)&&(*tmp != '\0')) {
tmp++;
p2++;
}
if (*p2 == '\0') return(p1-s1);
else {
p2 = s2;
p1++;
}
}
while (*(p1-1) != '\0');
return(-1);
}


void removestring(char *s,int i,int n)
{
char *p=s+i;

do {
*p = *(p+n);
if (*p != '\0') p++; /* only bump pointer if not finished */
}
while (*p != '\0');
}


void insertstring(char str1[], char str2[], int i)
{
int y, j, x, len1;
char tmp[20];
len1 = stringlength(str1);


/*copy str1 into tmp*/
for(y = i, x = 0; y <= len1; y++, x++)
tmp[x] = str1[y];

/*copy str2 into str1*/
for(j = 0; str2[j] != '\0'; j++)
str1[i + j] = str2[j];

/*copy tmp into str1*/
for (x = 0; tmp[x] != '\0'; x++)
str1[i + j +x] = tmp[x];

str1[i + j + x] = '\0';
}


void replacestring(char source[], char str1[], char str2[])
{
int i, n;

n = stringlength(str1);

i = findstring(&source, &str1);

removestring(&str1, i, n);

insertstring(source, str2, i);

}

 
Beware of *p you are using throughout your code. * is a dereferencing operator which would make your program crash if the pointer is not good (meaning NULL or uninitialized).

So, before dereferencing, be sure to test for p.
You reach the end of the string there with p and becomes NULL.

By the way, what are you doing in removeString?

Also, make the function prototypes present OUTSIDE of the main function of the program... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
#include<stdio.h>
#include<string.h>
#define CHARS 30

//declare functions
// in this case it is better to put those declations here
int stringlength (char str[]);
void insertstring (char source[], char str2[], int n);
int findstring (char* s1, char* s2);
void removestring (char *s, int i, int n);
void replacestring (char source[],char str1[], char str2[]);

void main(void)
{
//declare variable

char text[CHARS];

//copy string to char array text

strcpy(text,&quot; 1* for all and all for 1*&quot;);


replacestring(text, &quot;1&quot;, &quot;one&quot;);
replacestring(text, &quot;*&quot;, &quot;&quot;);
printf(&quot;%s\n&quot;, text);
}
int findstring(char *s1,char *s2)
{
char *p1=s1,*p2=s2,*tmp;

do {
while ((*p1 != *p2)&&(*p1 != '\0')) p1++;
tmp = p1;
while ((*tmp == *p2)&&(*tmp != '\0')) {
tmp++;
p2++;
}
if (*p2 == '\0') return(p1-s1);
else {
p2 = s2;
p1++;
}
}
while (*(p1-1) != '\0');
return(-1);
}


void removestring(char *s,int i,int n)
{
char *p=s+i;

do {
*p = *(p+n);
if (*p != '\0') p++; /* only bump pointer if not finished */
}
while (*p != '\0');
}


void insertstring(char str1[], char str2[], int i)
{
int y, j, x, len1;
char tmp[20];
len1 = stringlength(str1);


/*copy str1 into tmp*/
for(y = i, x = 0; y <= len1; y++, x++)
tmp[x] = str1[y];

/*copy str2 into str1*/
for(j = 0; str2[j] != '\0'; j++)
str1[i + j] = str2[j];

/*copy tmp into str1*/
for (x = 0; tmp[x] != '\0'; x++)
str1[i + j +x] = tmp[x];

str1[i + j + x] = '\0';
}

int stringlength (char str[])
{
int len = 0;
//write your code
return len;
}

void replacestring(char source[], char str1[], char str2[])
{
int i, n;

n = stringlength(str1);

i = findstring(source, str1); // here you dont need to use the operator '&'
// in the funtion parameters
removestring(str1, i, n);

insertstring(source, str2, i);

}

now the code will compile without any warnings of mistakes.
 
Thank you for the help. The pointers are driving me crazy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top