Thread: removing spaces in a string

  1. #1
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693

    removing spaces in a string

    // remove specified number of characters starting at

    // a specified index.

    // first int is starting index, second is count

    void removeString(char *, int, int);

    only using pointers and no brackets in the program.

    for example removestrng(char* test, 4, 6)

    text = "the wrong son" you move over 4 spaces to where the 'w' starts then remove 6 spaces out so the string would be "the son" in int main.

    i know how to get the characters where i want them I just do not know what kind of strategy to be used when deleting the spaces.
    P.S. this is all being done in a function nothing can be declared in main.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Read This .... C Board - Announcements in Forum : General Programming Boards

    Make an effort, post your code if you get stuck... otherwise...

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    i havnt written any code im still writing pseudo code on paper

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I think of it as a two part process, using two for loops, in the (I call it snip, because remove is a keyword), snip() function.

    First part is the overwrite of the char's from the start, to the length. The length serves as the offset of how far each char is moved over.

    Second part is the shift of the rest of the string, from the point where the last char was shifted, through until you reach the end of the string (including the end of string char).

    Hopefully, you've worked with pointers on strings before, at least a little. You'll find it challenging, otherwise. Remember that you can add an int to a pointer, no problem (and you should here), but you can't add two pointers. You can subtract two pointers, but it's not needed here.
    Last edited by Adak; 03-31-2011 at 12:24 PM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Well you can't really delete characters. What you do is move characters into the positions formerly occupied.

    Code:
    the wrong son
              |||
              ///
             ///
            ///
           ///
          ///
         ///
        ///
        |||
    the son
    So your task is to find out which is the first character to move... Starting with the one at 4+6, and move it to destination 4.
    Since you are not allowed indexing (square brackets), you need to keep a source pointer and destination pointer. Then don't forget to put a trailing null to signal the end of the string.

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    void removestrng(char *st1,int start,int numR)
    {

    int c1=0, c2=0, p=0;


    while(p<start)
    {c1++;
    *st1++;
    p++;
    }


    c2=c1+numR;


    printf("c1=%i\n", c1);
    printf("c2=%i\n", c2);
    printf("*st1=%c\n", *st1);
    printf("start=%i\n", start);







    }
    thats what I have so far just to see where my pointers and counters are at now I am stuck at how to shift the letters behind NULL because that is the way Im pretty sure you have to go about deleting the characters

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    String functions typically stop when they encounter a null character. So all you have to do is put the null where you want the end of the string to be. Anything beyond that will be ignored.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    question is how do I go about that involving random numbers being entered into main I cannot just hardcode it when someone else is using the program. would it be wise to use a while loop ?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wouldn't worry about random numbers, etc., just now. Focus on your called function. What the actual start and length of the chars are that gets removed, don't matter yet.

    That's one of the best things about functions - they encapsulate the logic for one part of the work to be done, into one certain area of the code.

    I wouldn't increment *st1. Instead, leave st1 as is, and simply "walk" through the chars, by using the loop counter. *(st1 + i) kind of expressions is what I would expect here.

    A while loop is OK, and perhaps more intuitive for the second loop, which will shift the remaining char's over to the left, by the length of the second int you pass into the function.

    The first loop should be a for loop imo, since it's start and ending point, is already exactly given by the parameters.
    Last edited by Adak; 03-31-2011 at 03:41 PM.

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    ok so a for loop such as this for(;*st1<start;*st1++)? i dont think I need to initialize *st1 in the beginning of the for loop since it is already given in main.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by camel-man View Post
    ok so a for loop such as this for(;*st1<start;*st1++)? i dont think I need to initialize *st1 in the beginning of the for loop since it is already given in main.
    Nope! Don't change *st1.At least, the way I do it (certainly not the only way), I use a loop iterator:

    Code:
    for(i=start;i<start + ?;i++)
    I'll bet you can figure out what goes in the ? spot.

  12. #12
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    once i walk through my chars how exactly can you move the null to a certain index. is it through an assignment operator? or will I have to use some kind of for loop and pointer to convert the null index to specified index.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's just a number. Assign it the same way you assign any number.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    sweet thanks so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 01:59 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM