Good afternoon everyone. I wanted to break the resolve of a program but I have an error and I can not fix it wanted to know your opinion about it. I'll pass the sentence and then the code for you to know I did.
Statement:
Implement strsubst without using library functions.
char * strsubst(char * s, const char * old_s, const char * new_s);
This function replaces the first occurrence of string s a old_s by new_s. The strings and old_s new_s may have different lengths. It is assumed that the memory space pointed to by s is sufficient to store the final result. The function returns the pointer s if you made the substitution s or NULL if not contains old_s.
Code:
#include <stdio.h>
char * strsubst(char * s, const char * old_s, const char * new_s)
{
char *aux=NULL;
int tamanhostring=40; // tamanho da string s;
int tamanho1=20; // tamanho da string old_s;
int i,j;
for(i=0; i <= tamanhostring; i++)
if(s[i] == *old_s)
*aux = i;
if(*aux)
{
for(j=0; j<tamanho1;j++)
s[(aux-s) + j] = new_s[j];
return (s);
}
else
return NULL;
}
int main()
{
char string[40],antiga[20],nova[20];
printf("\n");
printf("Entre com a string :");
gets(string);
printf("\n");
printf("Entre com a antiga palavra :");
gets(antiga);
printf("\n");
printf("Entre com a nova palavra :");
gets(nova);
printf("\n");
//printf("nova string ==> %s\n",strsubst(string,antiga,nova));
return(0);
}
Error:
Segmentation fault
Solution:
Well I've tried various solutions but can not find where the error in the main is he does not get to write the last printf is to comment so that the error must lie on the other method has changed many things but it does not. He wanted to know your opinion and what I should change my program to attain the objectives.