We are getting deeper and deeper of ctype.h and isspace() ,but no one is telling me ths solution !

Discussion in 'C Programming' started by Durgesh Sharma, Dec 21, 2004.

  1. Are there no genric Macros in c to represent Integers,Characters,...or
    other data types ?
    I want to pass that MACRO (representing an alpha numeric Character)to
    that strrchar() function,to get the last occurence of the last NON
    SPACE or Alphanumeric character,then i want to increase the position
    by one and add a NULL char ....This is my IDEA of rtrim
    function.....pls help me out.....

    How can i get a way to represent genric alphanumeric charecters ?

    Please help me.

    Thanks in Advance.
    regards,
    Durgesh
     
    Durgesh Sharma, Dec 21, 2004
    #1
    1. Advertising

  2. On Tue, 20 Dec 2004, Durgesh Sharma wrote:
    >
    > Are there no genric Macros in c to represent Integers,Characters,...or
    > other data types ?


    Data types correspond to sets of values that can be taken on by
    variables or expressions. Macros, in C, are purely lexical constructs
    with absolutely no "data type" from the language's point of view.

    > I want to pass that MACRO (representing an alpha numeric Character)to
    > that strrchar() function,


    There is no such function in the C standard library.

    > to get the last occurence of the last NON
    > SPACE or Alphanumeric character,then i want to increase the position
    > by one and add a NULL char ....This is my IDEA of rtrim
    > function.....pls help me out.....


    Do you mean to write

    char *rtrim(char *buffer)
    {
    char *end = strchr(buffer, '\0');
    while (end > buffer && isspace(end[-1])) --end; /* see [1] */
    *end = '\0';
    return buffer;
    }

    What's so hard about that?

    > How can i get a way to represent genric alphanumeric charecters ?


    Please define "genric alphanumeric charecters," and tell us why you
    want to "represent" them (it?). In other words, what are you /really/
    trying to do?

    -Arthur

    [1] - Yes, pedantically speaking I hear it ought to be
    while (end > buffer && isspace((unsigned char) end[-1])) --end;
    But I hardly ever write the cast in practice. I suppose I'm limiting
    the portability of my code to those implementations which do the Right
    Thing when you pass a 'char' to the routines in <ctype.h>, but IMVHO
    any implementation that doesn't do that is icky anyway. Add the cast
    if you want. :)
    (Maybe I ought to get in the habit of writing wrappers for the <ctype.h>
    functions: #define is_space(x) isspace((unsigned char)(x)) and so forth.
    ....Maybe.)
     
    Arthur J. O'Dwyer, Dec 21, 2004
    #2
    1. Advertising

  3. Durgesh Sharma

    Jack Klein Guest

    On 20 Dec 2004 22:16:03 -0800, (Durgesh Sharma)
    wrote in comp.lang.c:

    > Are there no genric Macros in c to represent Integers,Characters,...or
    > other data types ?
    > I want to pass that MACRO (representing an alpha numeric Character)to
    > that strrchar() function,to get the last occurence of the last NON
    > SPACE or Alphanumeric character,then i want to increase the position
    > by one and add a NULL char ....This is my IDEA of rtrim
    > function.....pls help me out.....
    >
    > How can i get a way to represent genric alphanumeric charecters ?
    >
    > Please help me.
    >
    > Thanks in Advance.
    > regards,
    > Durgesh


    I gave you a perfectly good answer, with useable code, when you first
    asked this question on December 4.

    Since then you spammed the group for a gmail invitation.

    *plonk*

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 21, 2004
    #3
  4. >Are there no genric Macros in c to represent Integers,Characters,...or
    >other data types ?


    No, and what does this question have to do with what follows?

    >I want to pass that MACRO (representing an alpha numeric Character)to
    >that strrchar() function,to get the last occurence of the last NON


    C does not have a function strrchar().
    C does have a function strrchr(). The second argument takes a
    specific (not generic, whatever a generic character would be)
    character cast to an int.

    >SPACE or Alphanumeric character,then i want to increase the position
    >by one and add a NULL char ....This is my IDEA of rtrim
    >function.....pls help me out.....


    Trying to use strrchr() for an rtrim function is a lot like
    using a screwdriver to remove nails: it's the wrong tool for
    the job. strrchr() scans for the last occurrence of a specific
    character, not a generic character (whatever that is).

    >How can i get a way to represent genric alphanumeric charecters ?


    Regardless of how you represent them, strrchr() won't accept
    them. ANSI C does not have regular expressions. And even if
    you use one of the ways of representing regular expressions
    (say "[A-Za-z0-9]*"), strrchr() doesn't take a string for
    its second argument.

    >Please help me.


    Hint: loop over each character of the string. If it's NOT one of
    the characters you want to trim off (the <ctype.h> functions may
    be helpful here), save this position. After the loop exits, the
    last saved position points at the last character you wish to keep.
    Now, think about what you want to happen if NONE of the characters
    you want to keep (the string is all blanks). Also, what is supposed
    to happen if the string ends in a non-space, non-alphanumeric
    character (say, a period, comma, colon, question mark, or asterisk)?

    Gordon L. Burditt
     
    Gordon Burditt, Dec 21, 2004
    #4
  5. Durgesh Sharma

    infobahn Guest

    Re: We are getting deeper and deeper of ctype.h and isspace() ,butno one is telling me ths solution !

    Durgesh Sharma wrote:
    > Are there no genric Macros in c to represent Integers,Characters,...or
    > other data types ?


    If you mean a sort of regex for characters, then the answer is no.

    > I want to pass that MACRO (representing an alpha numeric Character)to
    > that strrchar() function,to get the last occurence of the last NON
    > SPACE or Alphanumeric character,then i want to increase the position
    > by one and add a NULL char ....This is my IDEA of rtrim
    > function.....pls help me out.....


    In fact, you want a sort of strrspn function. Alas, there isn't one.
    But it would be quite easy to write.

    Or you could define an array of UCHAR_MAX + 1 ints, with each one
    set to either 1 or 0 (or, perhaps, you could bitmap them into various
    categories such as digit, hexit, alpha, lower, upper, etc), and then
    write your own macros. In fact, that's quite a neat idea.

    > How can i get a way to represent genric alphanumeric charecters ?


    Develop one.
     
    infobahn, Dec 21, 2004
    #5
  6. Re: We are getting deeper and deeper of ctype.h and isspace() ,butno one is telling me ths solution !

    Durgesh Sharma wrote:

    > Are there no genric Macros in C
    > to represent Integers,Characters,...or other data types?


    No.

    > I want to pass that MACRO (representing an alpha numeric Character)to
    > that strrchar() function,to get the last occurence of the last NON
    > SPACE or Alphanumeric character, then I want to increase the position
    > by one and add a NULL char ....This is my IDEA of rtrim
    > function.....pls help me out.....
    >
    > How can I get a way to represent genric alphanumeric charecters?


    You can't.

    > Please help me.


    > cat rtrim.c

    #include <ctype.h>
    #include <string.h>

    char* rtrim(char* s) {
    const
    size_t n = strlen(s);
    size_t j = n;
    while ((0 < j) && isspace(s[j-1]))
    --j;
    s[j] = '\0';
    return s;
    }
     
    E. Robert Tisdale, Dec 21, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. str.isspace()

    , Sep 3, 2006, in forum: Python
    Replies:
    3
    Views:
    495
    George Sakkis
    Sep 3, 2006
  2. Michel Rouzic

    Telling an empty binary file from a "full" one

    Michel Rouzic, Sep 19, 2005, in forum: C Programming
    Replies:
    26
    Views:
    640
    Chris Torek
    Sep 24, 2005
  3. Adrian
    Replies:
    2
    Views:
    328
    Adrian
    Nov 14, 2006
  4. Army1987

    Can isspace('\0') ever be true?

    Army1987, Jul 15, 2007, in forum: C Programming
    Replies:
    7
    Views:
    351
    Army1987
    Jul 15, 2007
  5. Replies:
    19
    Views:
    554
    Alf P. Steinbach
    Jan 30, 2008
Loading...

Share This Page