Thread: How do you create a function that removes certain areas from a string?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    196

    How do you create a function that removes certain areas from a string?

    Hello guys, i'm having serious issues trying to create a function that removes certain sentences or parts from a sentence.

    Most of the time, I try my luck in doing the assignment since I feel I've started to get the concept but this time, I'm completely stump on how to do this.

    This is all I've done, and it's nothing. This is just the easy part.
    How do you create a function that removes certain areas from a string?-screen-shot-2013-11-04-9-54-08-pm-png
    Code:
    #include <stdio.h>#include <string.h>
    
    
    int main()
    {
        char text[] = "The lunatic, the lover, and the poet, are of imagination all compact.";
        printf("Before : %s\n", text);
        printf("After  : %s\n", text);
        return 0;
    }
    The output is supposed to be

    The lunatic, the poet, are of imagination all compact.

    I'm not looking for an answer, since this won't help me at all, but a way on how to do this.




    Last edited by Cdd101; 11-04-2013 at 11:02 PM.

  2. #2
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Hey guys, I'll get back at you tomorrow if I still need help. Going to try to do it by myself a little longer.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I know if I were to do this, I'd start with a pencil and paper (in this case, graph paper would be a better choice for me personally). Seeing the letters in a grid would help visualize the process, from which an algorithm could be developed.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    If there's no restrictions in the homework, you could do this using memmove().

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by KCfromNC View Post
    If there's no restrictions in the homework, you could do this using memmove().
    Well when he asks for a function, he wants us to make our own functions.
    This is what I've done so far but I'll get back to you guys later
    Code:
    int removeString(int argc, char const argv[])
    {
        char word[200];
        int i;
        long int len = strlen(word);
        int rem = 13;
        int out = 15;
        /* remove rem'th char from word */
        for (i = rem; i < len - 1; i++) word[i] = word[i + out];
        if(i < len) word[i] = '\0';
        return 0;
    }
    
    
    int main ()
    {
        char word[] = "The lunatic, the lover, and the poet, are of imagination all compact.";
        
        printf("%s\n", word);
        removeString(15,13); //incompatible integer to pointer conversion error 
        return 0;
    }
    Im still working on it.

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    If i move my function into the main it works though :/
    Code:
    int main (){
        char word[] = "The lunatic, the lover, and the poet, are of imagination all compact.";
        
        int i;
        long int len = strlen(word);
        int rem = 13;
        int out = 15;
        /* remove rem'th char from word */
        for (i = rem; i < len - 1; i++) word[i] = word[i + out];
        if(i < len) word[i] = '\0';
        
        printf("%s\n", word);
        return 0;
    }
    It prints "The lunatic, the poet, are of imagination all compact."

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In post #6, you're trying to access elements way outside the bounds of your array. I added a line for you:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char word[] = "The lunatic, the lover, and the poet, are of imagination all compact.";
        int i;
        int len = strlen(word);
        int rem = 13;
        int out = 15;
    
        /* remove rem'th char from word */
        for (i = rem; i < len - 1; i++)
        {
            word[i] = word[i + out];
            printf("word[i + out] is accessing element %2d out of %d\n",i+out,sizeof(word)); /* added */
        }
        if(i < len) word[i] = '\0';
    
        printf("%s\n", word);
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In post #5:

    - You're not passing the expected values to your function (it expect an int and a char array) - hence the warning
    - You don't use the char array you pass, but instead use another char array ("word") that is not initialized

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    It prints what I wanted.
    Code:
    int returnString(char word[], int x, int y)
    {
        int i;
        long int len = strlen(word);
        int rem = x;
        int out = y;
        /* remove rem'th char from word */
        for (i = rem; i < len - 1; i++) word[i] = word[i + out];
        if(i < len) word[i] = '\0';
        return 0;
    }
    
    
    int main ()
    {
        char text[] = "The lunatic, the lover, and the poet, are of imagination all compact.";
        returnString(text, 13, 15);
        printf("%s\n", text);
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Not sure why you posted it, but the one big thing I noticed:
    Code:
    int returnString(char word[], int x, int y)
    {
    ...
        int rem = x;
        int out = y;
    What's wrong with ditching the local variables and naming your parameters rem and out?
    Code:
    int returnString(char word[], int rem, int out)
    Or better yet, name your function removeString, per the directions, and pick even more descriptive names for the parameters (it can accept a string with multiple words, or no words at all; rem might be remove, but what is out?). And it doesn't need to return anything, so make it void:
    Code:
    void removeString(char text[], int start_index, int remove_length)
    Other than that, does it work? Did you test it? Did you test it in ways that can break it? Like start_index is greater than the length of the text? What about ways to break it with different values of remove_length? What happens if start_index or remove_length are negative? Perhaps unsigned int would be better.

  11. #11
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    While I couldn't get this homework to work yesterday, I was working on this one.

    Code:
    void insertString(char string[], char c[], int idx)
    {
        //long int len = strlen(string);
        char stringtwo[2000] = "";
        int i;
        i = 0;
        while ((string[i] = c[i]) != '\0') {
            i++;
            strcpy(stringtwo,string);
            strcpy(stringtwo,c);
        }
        printf("%s", stringtwo);
    }
    
    
    int main()
    {
        char text[ 200 ] = "Doubt I love.";
        insertString(text, "thou the stars are fire;\n Doubt that ", 5);
        insertString(text, "the sun doth move;\n Doubt ", 42);
        insertString(text, "truth to be a liar;\n But never doubt ", 68);
        printf(" %s\n", text );
    }
    This is what it prints
    Code:
    thou the stars are fire;
     Doubt that the sun doth move;
     Doubt truth to be a liar;
     But never doubt  truth to be a liar;
     But never doubt
    This is what it's supposed to print.
    Code:
    {Doubt }thou the stars are fire;
    Doubt that the sun doth move;
    Doubt truth to be a liar;
    But never doubt {I love.}

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Cdd101 View Post
    It prints what I wanted.
    This time, perhaps. But that's what happens with undefined behavior - it can seem to work as you want it, or anything else can happen. Who's to say it will work right when your professor tries running it on their setup?

    J.2 Undefined behavior
    The behavior is undefined in the following circumstances:

    ...

    An array subscript is out of range, even if an object is apparently accessible with the
    given subscript (as in the lvalue expression a[1][7] given the declaration int
    a[4][5]) (6.5.6).

  13. #13
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    Not sure why you posted it, but the one big thing I noticed:
    Code:
    int returnString(char word[], int x, int y)
    {
    ...
        int rem = x;
        int out = y;
    What's wrong with ditching the local variables and naming your parameters rem and out?
    Code:
    int returnString(char word[], int rem, int out)
    Or better yet, name your function removeString, per the directions, and pick even more descriptive names for the parameters (it can accept a string with multiple words, or no words at all; rem might be remove, but what is out?). And it doesn't need to return anything, so make it void:
    Code:
    void removeString(char text[], int start_index, int remove_length)
    Other than that, does it work? Did you test it? Did you test it in ways that can break it? Like start_index is greater than the length of the text? What about ways to break it with different values of remove_length? What happens if start_index or remove_length are negative? Perhaps unsigned int would be better.
    My bad about using meaningless variable names.

    Is this what you're asking me to do?
    Code:
    #include <stdio.h>#include <string.h>
    
    
    void removeString(char text[], int start_index, int remove_length)
    {
        int i;
        long int length = strlen(text);
        for (i = start_index; i < length - 1; i++) text[i] = text[i + remove_length];
        if(i < length) text[i] = '\0';
    }
    
    
    int main ()
    {
        char text[] = "The lunatic, the lover, and the poet, are of imagination all compact.";
        printf("Before: %s\n", text);
        removeString(text, 13, 15);
        printf("After: %s\n", text);
        return 0;
    }
    It works.
    Last edited by Cdd101; 11-05-2013 at 12:17 PM.

  14. #14
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Well it seems in my homework_2 i had such an idiotic mistake

    I'm able to at least make it a little better now.

    Code:
    void insertString(char text[], char c[], int idx) {
        char stringtwo[2000] = "";
        int i;
        i = 0;
        while ((text[i] = c[i]) != '\0') {
            i++;
            strcpy(stringtwo,text);
            strcpy(stringtwo,c);
        }
        printf("%s", stringtwo);
    }
    
    
    int main()
    {
        char text[ 200 ] = "Doubt I love.";
        insertString(text, "thou the stars are fire;\n Doubt that ", 5);
        insertString(text, "the sun doth move;\n Doubt ", 42);
        insertString(text, "truth to be a liar;\n But never doubt ", 68);
        printf(" %s\n", text );
     
    }
    Not the desired out put I want though yet.

    Code:
    thou the stars are fire;
     Doubt that the sun doth move;
     Doubt truth to be a liar;
     But never doubt  truth to be a liar;
     But never doubt

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Cdd101 View Post
    My bad about using meaningless variable names.

    Is this what you're asking me to do?
    Yep, that's what I was suggesting. Makes your code much easier to read and understand, which means you're less likely to make mistakes.
    Quote Originally Posted by Cdd101 View Post
    It works.
    Well, you've proven it works for that one test case, is that really sufficient? What if you change start_index to 60? What if remove_length is longer than the actual string? What about passing in negative values? Does it still work? Is it possible somebody might call your funciton with invalid parameters (e.g. your teacher when testing)? Should you be doing any more error checking? The assignment doesn't say one way or the other, so I would err on the side of caution. Maybe email your prof to be sure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving between areas
    By Aliaks in forum C++ Programming
    Replies: 100
    Last Post: 07-27-2009, 02:23 AM
  2. printf removes \
    By MK4554 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2006, 12:40 PM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. rescaling areas
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 02-28-2006, 03:22 PM
  5. volumes and areas
    By oscuro in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2002, 10:27 PM