Thread: A lil help Please

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    6

    A lil help Please

    So I've got this Problem I'm working on and I get 50 extra credit points on my final if I can solve the thing, but I can't get it to compile in CodeBlocks. Any help would be greatly appreciated

    Here's the Question

    Code:
    Write a function named remove() that deletes all occurrences of a character from a string. The function should take two arguments : the string name and the character to be removed. For example, if the message contains the string HappyHolidays, the function call remove(message, 'H') should place the string appyolidays into message.
    Here's my code so far

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void removeChar(char *str, char garbage) {
    
    
        char *src, *dst:
        for (src = dst =str; *src != '\0'; src++){
            *dst = src;
            if (*dst != garbage) dst++;
        }
        *dst = '\0';
    }
    int main(void)
    {
    char* str = malloc(strlen("HappyHolidays")+1);
        strcpy(str, "HappyHolidays");
        removeChar(str, 'H');
        printf("%s", str);
        free(str);
        return 0;
    
    
    }

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    it helps to know how to read and under stand warnings and errors but I do not use code blocks anymore so I forget how they display theres,
    Code:
    gcc -Wall -Wextra -Wpedantic -lm -o "term2" "term2.c" (in directory: /home/userx/bin)
    term2.c: In function 'removeChar':
    term2.c:9:20: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token
         char *src, *dst: 
                        ^
    term2.c:12:26: warning: statement with no effect [-Wunused-value]
         for (src = dst =str; *src != '\0'; src++){
                              ^
    term2.c:12:45: error: expected ';' before ')' token
         for (src = dst =str; *src != '\0'; src++){
                                                 ^
    term2.c:12:45: error: expected statement before ')' token
    term2.c:13:10: error: 'dst' undeclared (first use in this function)
             *dst = src;
              ^
    term2.c:13:10: note: each undeclared identifier is reported only once for each function it appears in
    term2.c:6:23: warning: unused parameter 'str' [-Wunused-parameter]
     void removeChar(char *str, char garbage) {
                           ^
    Compilation failed.
    you code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
     
    void removeChar(char *str, char garbage) {
     
     
        char *src, *dst: //< what the hell is that a colon or semi colon?,  hummm I wonder.
        
        
        for (src = dst =str; *src != '\0'; src++){
            *dst = src;
            if (*dst != garbage) dst++;
        }
        *dst = '\0';
    }
    int main(void)
    {
    char* str = malloc(strlen("HappyHolidays")+1);
        strcpy(str, "HappyHolidays");
        removeChar(str, 'H');
        printf("%s", str);
        free(str);
        return 0;
     
     
    }
    sometimes I even see my errors in the code after I post and look at it, getting a different perspective on it then wonder why I cannot just delete that mistake from my life.

    here is another one of your mistakes
    Code:
    1
    Write a function named remove()
    what is your function called? Pay attention to details I am sure your instructor will mark you on it. I know I would.
    Last edited by userxbw; 12-11-2017 at 01:01 PM.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    Obviously, I'm probably far from getting the answer on this @userxbw

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    Thanks for Replying so quickly @userxbw

    I'm probably so far out of the realm of coding this one.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Though I must say you know how to use google to your advanage it is your retyping other code you need to work on.

    How to remove the character at a given index from a string in C? - Stack Overflow

    answer 32 if anyone wants to see how this person knows how to try and put what is called reusable code to work in their favor.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Br3tt96 View Post
    Thanks for Replying so quickly @userxbw

    I'm probably so far out of the realm of coding this one.
    out of the realm of figuring out how to get that code some one else wrote to work you might say. not that I think that is a bad thing using what has already been coded. it is just then taking that knowledge and knowing how to use it again somewhere else.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Quote Originally Posted by userxbw View Post
    Code:
    Write a function named remove()
    what is your function called? Pay attention to details I am sure your instructor will mark you on it. I know I would.
    The instructor is an idiot since stdio.h already has a function called remove.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by john.c View Post
    The instructor is an idiot since stdio.h already has a function called remove.
    I need a like button
    Last edited by GReaper; 12-11-2017 at 01:58 PM.
    Devoted my life to programming...

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I just capitalized it
    Remove(char *string, ....)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*
     * Write a function named remove() that deletes all occurrences of a character from a string.
     *  The function should take two arguments : the string name and the character to be removed. 
     * For example, if the message contains the string HappyHolidays, the function call remove(message, 'H')
     *  should place the string appyolidays into message.
     */
     
    
    void Remove(char *string, char removeLetter)  
    {
          char *src, *dst;
        for (src = dst = string; *src != '\0'; src++) {
            *dst = *src;
            if (*dst != removeLetter) dst++;
        }
        *dst = '\0';
     
    }
    int main(void)
    {
        char string[100];
        char removeLetter;
        
        printf("Enter a string\n");    
        
        fgets(string, sizeof string, stdin);
        
        printf("Enter a letter within the same\n"
                "string you want removed\n");
        
        scanf(" %c", &removeLetter);
        
        Remove(string, removeLetter);
        
        printf("leftovers:: %s\n", string); 
            
       
    return 0;
    }
    Last edited by userxbw; 12-11-2017 at 02:07 PM.

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    Quote Originally Posted by john.c View Post
    The instructor is an idiot since stdio.h already has a function called remove.
    You have absolutley no idea. We had 15 chapters and we only covered 7, we were suppose to have 15 labs and we only had 7, and this garbage is from chapters we did not do at all or cover in any way. I'm basically screwed on my Final if I can't solve this for the 50pts.

    I've also got two problems I have to solve before tomorrow we've never been taught. One has a file that we have to code to pull from a file
    Last edited by Br3tt96; 12-11-2017 at 02:01 PM.

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Br3tt96 View Post
    You have absolutley no idea. We had 15 chapters and we only covered 7, we were suppose to have 15 labs and we only had 7, and this garbage is from chapters we did not do at all or cover in any way. I'm basically screwed on my Final if I can't solve this for the 50pts.

    I've also got two problems I have to solve before tomorrow we've never been taught. One has a file that we have to code to pull from a file
    look at post 9 it is modified to make it more flexible.
    edit
    this was the only two mistakes in the code you posted
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
      
      
    void removeChar(char *str, char garbage) {
      
      
        char *src, *dst; //< what the hell is that a colon or semi colon?,  hummm I wonder.
         
         
        for (src = dst =str; *src != '\0'; src++){
            *dst = *src; // missing pointer
            if (*dst != garbage) dst++;
        }
        *dst = '\0';
    }
    int main(void)
    {
    char* str = malloc(strlen("HappyHolidays")+1);
        strcpy(str, "HappyHolidays");
        removeChar(str, 'H');
        printf("%s\n", str);
        free(str);
        return 0;
      
      
    }
    Last edited by userxbw; 12-11-2017 at 02:14 PM.

  12. #12
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    Quote Originally Posted by userxbw View Post
    look at post 9 it is modified to make it more flexible.
    Found this online and it works lol

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    
    void del(char str[], char ch);
    
    
    void main() {
       char str[10];
       char ch;
    
    
       printf("\nHappyHolidays : ");
       gets(str);
    
    
       printf("\nEnter character which you want to delete : ");
       scanf("%ch", &ch);
    
    
       del(str, ch);
       getch();
    }
    
    
    void del(char str[], char ch) {
       int i, j = 0;
       int size;
       char ch1;
       char str1[10];
    
    
       size = strlen(str);
    
    
       for (i = 0; i < size; i++) {
          if (str[i] != ch) {
             ch1 = str[i];
             str1[j] = ch1;
             j++;
          }
       }
       str1[j] = '\0';
    
    
       printf("\ncorrected string is : %s", str1);
    }

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you need to fix post 12 before your hour has expired.

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Br3tt96 View Post
    Found this online and it works lol

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    
    void del(char str[], char ch);
    
    
    void main() {
       char str[10];
       char ch;
    
    
       printf("\nHappyHolidays : ");
       gets(str);
    
    
       printf("\nEnter character which you want to delete : ");
       scanf("%ch", &ch);
    
    
       del(str, ch);
       getch();
    }
    
    
    void del(char str[], char ch) {
       int i, j = 0;
       int size;
       char ch1;
       char str1[10];
    
    
       size = strlen(str);
    
    
       for (i = 0; i < size; i++) {
          if (str[i] != ch) {
             ch1 = str[i];
             str1[j] = ch1;
             j++;
          }
       }
       str1[j] = '\0';
    
    
       printf("\ncorrected string is : %s", str1);
    }
    that is basically what the other one is doing one uses pointer math and the other shifts letters around, now you got two working ones to pick from or finagle a turn in of two to get 100 points.

  15. #15
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    Quote Originally Posted by userxbw View Post
    that is basically what the other one is doing one uses pointer math and the other shifts letters around, now you got two working ones to pick from or finagle a turn in of two to get 100 points.
    Dude, you're awesome man! I'll probably use one and share one with another person who's lost in my class

    Thank you so much!

Popular pages Recent additions subscribe to a feed

Tags for this Thread