-1

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.

J...S
  • 5,079
  • 1
  • 20
  • 35
  • `calloc(0,sizeof(char));` doesn't give you enough space to use with `scanf("%s", texto)` – David Ranieri May 02 '18 at 21:07
  • If you put `scanf("%c",&del)` after the string input, it will take the newline remaining in the input buffer. Put a space before `%c` as in `scanf(" %c",&del)` because unlike most format specifiers, `%c` does not automatically skip whitespace remaining from the previous input. This one of the most frequent C problems. – Weather Vane May 02 '18 at 21:31
  • Use good names instead of i,j. Also, there is no reason to pass 0,0 as the initial values, they should be declared and initialized inside the routine. – stark May 02 '18 at 21:48

3 Answers3

0

You are only allocating space for one char

texto = (char*)calloc(0, sizeof(char));

You need to allocate enough space for your entire string, including NULL terminator, this will let you have a string of 20 chars plus a NULL terminator

texto = (char*)calloc(21,  sizeof(char));

And you should at the very least control how many characters you can read into your string in scanf() so you can't overflow your buffer

scanf("%20s", texto);

For safer and more robust user input, you should look up fgets().

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
0

The problem is that you have redeclared del in line:

char del;
scanf("%c",&del);

So just remove second deceleration of del.

cse
  • 4,066
  • 2
  • 20
  • 37
0

You have problem reading del after reading the string because, scanf() used to read the string leaves the final \n in the input buffer unconsumed if your format string is like

scanf("%s", stringname);
scanf("%c", charactername);

You could solve this by

scanf("%s", stringname);
scanf(" %c", charactername);

adding a space before the %c in the scanf() used to read the character. This will consume any white space character still left in the input buffer before reading the actual character.

This is because for %c leading white space is not automatically skipped.

Note that you are allocating memory only for a single character with

char* texto;
texto=(char*)calloc(0,sizeof(char));

instead you could do

char* texto;
texto=calloc(0,sizeof(char)*10);

where 10 is the size of the string including the \0 character. So you can read maximum 9 characters into texto otherwise overflow will occur.

To avoid the overflow, while reading with scanf() use a width specifier as in

scanf("%9s", texto);

You don't need to explicitly cast the return value of calloc() in C. The conversion to char * from the void * returned by calloc() will be done implicitly.

And to delete you could just do

char* deleteChar(char* texto,int n,char del,int i,int j)
{
    char *ptr=strchr(texto, del);
    if(ptr!=NULL)
    {
        sprintf(texto, "%.*s%s", ptr-texto, ptr+1);
    }   
}

You don't need to return texto because what was passed is an array.

char* deleteChar(char* texto,int n,char del,int i,int j)
{
    char *ptr=strchr(texto, del);
    if(ptr!=NULL)
    {
        sprintf(texto, "%.*s%s", ptr-texto, texto, ptr+1);
    }   
}

strchr() is used to get the first occurrence of a character in a string.

Also, the return type of main() shouldn't be void. See Return type of main function.

J...S
  • 5,079
  • 1
  • 20
  • 35