string-manipulating function

This is a discussion on string-manipulating function within the C++ Programming forums, part of the General Programming Boards category; hi everyone i'm trying to write a function with header void formatline (char line [50]) that does the following things: ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    string-manipulating function

    hi everyone

    i'm trying to write a function with header void formatline (char line [50]) that does the following things:

    - if a comma in line is preceded by a space, it'll delete that space.

    - if the colon (there will definitely be one in line) is not preceded by a space, it will insert one space before it.

    (parameter line might have commas, but it will always have a colon. and there is no more than one space in between any two characters in line.)
    so the function will go through line, and when it gets to a comma, it'll check if the character before it is a space, if so, it'll delete that space. and when it gets to the colon, it will check if the character before that is a space, if not it will insert one there.

    my problem now is that i don't know if there's a way you can just delete an element of a character array, or insert one.. (by the way using a linked list isn't an option because this has to take in a character array).. how do i do it?

    thanks in advance
    linette

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Search through the string. If you encounter a space-comma element, simply move every element after that one one step to the left.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Don't have much time to respond at the moment so I can't answer your question but I would like to point out a problem that I saw immediately with the function definition.

    Code:
    void formatline (char line [50]);
    As you have it, the variable line is a character array. As such, when the code runs, a new variable line will be created local to the function formatline. This local variable will be manipulated in some way by the function. I am assuming you wish to call this function from some other piece of code and then use the modified string after calling the function. The problem with this, is that the function is going to modify the local copy of the character array and not the one that exists in whatever piece of code called the formatline function. After you have called the function, whatever variable you passed into the function is going to be the exact same thing as before the function call. You will probably need to change the function header like so:

    Code:
    void formatline( char* line );
    With the variable line as a pointer, the changes to the character array you pass into the function will remain in effect after the function has completed. You will call this function exactly like you would your original version, i.e.:

    Code:
    char buffer[50];
    ...
    ...
    ...
    formatline( buffer );
    Anyway, if your intent wasn't to have this modified character array available after the function call, then ignore the above.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    thanks, both of you

    magos, can you please show me (with code) how to do it?

    and hkmp5kpdw (boy you have a difficult username ), that's right! thanks for reminding me of that..

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    left shift by 1 if letter b
    
    char line[30] = "happy birthday"
    for(i = 0; i < strlen(line); i++)
    {
      if(line[i] == 'b')
      {
         for(j = i; j < strlen(line); j++)
         line[j] = line[j + 1];
       }
    }
    
    right shift by 1 if letter 'a' and insert * where letter 'a' was
    
    for(i = 0; i < strlen(line); i++)
    {
      if(line[i] == 'a')
      {
         for(j = strlen(line); j < i; j--)
         {
            line[j + 1] = line[j];
         }
          line[i++] = '*';
       }
    }
    Last edited by elad; 04-01-2002 at 03:27 PM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    hk_mp5kpdw, are you sure about this? I think it modifies it either way.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    thanks elad!

    the two for loops you posted - i modified the first one a little so it's fine now for "deleting" the space before a comma. but the second one:
    Code:
    for(c = 0; c < strlen(line); c++)
    {
      if(line[c] == 'a')
      {
         for(j = strlen(line); j < c; j--)
         {
            line[j + 1] = line[j];
         }
          line[c++] = '*';
       }
    }
    just replaces 'a' with '*', it doesn't shift anything over..

    it's probably in the second for loop header, where you say j<c, i think that prevents it from getting into that loop.. what should i change it to?..

    actually no..!.. if 'a' is being replaced by '*' with that statement (line[c++]...) that should mean that it has been shifted over once (to the right).. but why does the output look exactly like the original string only with 'a' replaced..?.. i'm confused!..
    Last edited by Linette; 04-01-2002 at 04:32 PM.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    71

    done!

    it seems i made the right changes; it works now!

    thanks for your help anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21