Thread: reduce space char function question..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    reduce space char function question..

    i need to write a function which transforms 2 or more spaces into one
    for example:
    "gjkk jjj k" (for some reason it doesnt show my edit) it should be with much more spaces between

    turns to
    "gjkk jjj k"

    i have this structure
    Code:
    void *(char *str)
    {
        int i;
        for(i=0;str[i];i++)
             if(str[i]=='i')
                    if (??1??||??2??||??3??)
                    {
                        strcpy(??4??);
                        ??5??;
                     }
    }
    i need to substitute code instead of ??1?? for this function to work
    i dont know why they use 3 or statements on this if??
    i would do it by if the current cell is ' ' ans the next to then ..

    and str copy makes of one string as the copy of abother
    how to use it??
    Last edited by transgalactic2; 03-19-2009 at 02:48 PM.

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    If you're trying to duplicate strings, then you have to be able to resize the string and insert characters. Let's see . . .

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[]) {
        char string[128] = "a b c d e f";
        size_t x, y;
        for(x = 0; string[x]; x ++) {
            if(string[x] == ' ') {
                for(y = strlen(string); y > x; y --) {
                    string[y] = string[y-1];
                }
                string[x+1] = ' ';
        }
        printf("%s\n", string);
        return 0;
    }
    Or something like that. That may need to be strlen(string)-1, not sure.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    OP wants to get rid of spaces not insert them. I suppose
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    yes i want to have only one space between two other chars

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Ohhh, sorry. My bad. I misread the question.

    Try something more like this, then.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[]) {
        char string[128] = "a b c d e f";
        size_t x, y;
        for(x = 0; string[x]; x ++) {
            while(string[x] == ' ') {
                for(y = x; y < strlen(string); y ++) string[y] = string[y+1];
            }
        }
        printf("%s\n", string);
        return 0;
    }
    Easier, actually.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need it to be like in the structure i was given
    i need to say
    what did i put instead of each ??1?? stuff

    ??

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    i need it to be like in the structure i was given
    i need to say
    what did i put instead of each ??1?? stuff

    ??
    what this line

    if(str[i]=='i')

    has to do with searching for spaces?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    What I gave you is how to reduce any amount of spaces (or any char you like) into no spaces. It is easy enough to modify to reduce to one single space.

    Modifying it into the form you were given is not my problem. I don't understand exactly what you're asking for, but AFAICT, you've been given this as part of a homework problem, and your function has to meet the structure that you posted above.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I prefer something along the lines of:

    Code:
    char *s, *d;
    s = d = str;
    while(*s) {
      if(*s != ' ')
        *d++ = *s;
      s++;
    }
    *d = *s;
    That's a lot faster than copying the rest of the string for every space.

  10. #10
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    True enough, EVOEx. Also harder to follow if you don't understand strings and pointers very well, however.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by vart View Post
    what this line

    if(str[i]=='i')

    has to do with searching for spaces?
    i dont know thats what ive been given

    i need it to be like in the structure i was given
    i need to say
    what did i put instead of each ??1?? stuff

    ??

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    i dont know thats what ive been given
    I doubt it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    do doubt it
    it write from an exam

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by transgalactic2 View Post
    do doubt it
    it write from an exam
    you should reread it. Or you are missing reference to 'i' char in task description, or your code has a typo
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Presumably if it's an old exam (and you are not CURRENTLY in an exam), you should be able to get the old answers too?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM