Home > database >  Removing char from string C
Removing char from string C

Time:11-30

I'm learning C now I need to make a program that remove char that I'll input from string. I've seen an algorithm and I write this code

#define MAX_LEN 200
int main()
{
    char str[MAX_LEN];
    char rem;
    int i = 0;
    
    printf("Enter the setence:");
    gets(str);
    printf("\nEnter the char to remove");
    rem = getchar();
    char* pDest = str;
    char* pS= str;
    printf("sent:\n%s", str);

    while (str[i]!='\0'){
        if (*pDest != rem) {
            *pDest = *pS;
            pDest  ;
            pS  ;
        }
        else if (*pDest == rem) {
            pS  ;
        }
        i  ;
    }
    *pDest = '\0';
    while (str[i] != '\0') {
        printf("number%d", i);
        putchar(str[i]);
        printf("\n");
        i  ;
    }
}

But it returns nothing, like the value str gets, i think \0 and retuns nothing. May you help me to find the problem?

CodePudding user response:

Use functions!!

If dest is NULL then this function will modify the string str otherwise, it will place the string with removed ch in dest.

It returns reference to the string with removed character.

char *removeChar(char *dest, char *str, const char ch)
{
    char *head = dest ? dest : str, *tail = str;

    if(str)
    {
        while(*tail)
        {
            if(*tail == ch) tail  ;
            else *head   = *tail  ;
        }
        *head = 0;
    }
    return dest ? dest : str;
}


int main(void)
{
    char str[] = "ssHeslsslsos sWossrlssd!ss";

    printf("Removal of 's' : `%s`\n", removeChar(NULL, str, 's'));
}

CodePudding user response:

It would be easier to use array style indexing to go through the string. For example use str[i] = str[i 1] instead of *pstr = *other_pstr. I leave this incomplete method, since this looks like homework.

int main()
{
    char str[] = "0123456789";
    char ch = '3';
    for (int i = 0, len = strlen(str); i < len; i  )
        if (str[i] == ch)
        {
            for (int k = i; k < len; k  )
            {
                //Todo: shift the characters to left 
                //Hint, it's one line
            }
            len--;
        }
    printf("%s\n", str);
    return 0;
}

CodePudding user response:

I just added new char array char dest[MAX_LEN] that store string with deleted symbols:

 #define MAX_LEN 200
    int main()
    {
        char str[MAX_LEN];
        char rem;
        int i = 0;
        
        printf("Enter the setence:");
        gets(str);
        printf("\nEnter the char to remove");
        rem = getchar();
        char dest[MAX_LEN] = "\0";
        char* pDest = dest;
        char* pS = str;
        printf("sent:\n%s", str);
    
        while (str[i]!='\0')
        {
            if (*pS != rem) 
            {
                *pDest = *pS;
                pDest  ;
                pS  ;
            }
            else if (*pS == rem) 
            {
                pS  ;
            }
            i  ;
        }
        i = 0;
        printf("\nres:\n %s \n", dest);
        while (dest[i] != '\0') {
            printf("number%d", i);
            putchar(dest[i]);
            printf("\n");
            i  ;
        }
    }
  • Related