So I made a program for deleting a char from a string, it looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* deleteChar(char* texto,int n,char del,int i,int j)
{
/*for(i=0,j=0;i<n;i++)
if(texto[i]!=del)
{
texto[j]=texto=i;
j=+1;
}
for(n-i;n-i<n;i--)
texto[n-i]=NULL;
return(texto);*/
if(i!=n)
{
if(texto[i]!=del)
{
texto[j]=texto[i];
j+=1;
}
i+=1;
texto=deleteChar(texto,n,del,i,j);
}
else
{
i=i-j;
for(n-i;n-i<n;i--)
texto[n-i]=NULL;
return(texto);
}
}
void main()
{
char del;
printf("Remover: \n");
scanf("%c",&del);
char* texto;
texto=(char*)calloc(0,sizeof(char));
printf("Texto: \n");
scanf("%s",texto);
int n=0;
n=strlen(texto);
/*char del;
scanf("%c",&del);*/
texto=deleteChar(texto,n,del,0,0);
printf("%s ",texto);
}
Focus on main()
, for some reason if I scanf("%c",&del)
after getting my string, the program breaks (before even getting the del
input'), but if I do it after it works greatly.
I have no idea why.