By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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.)
Nov 14 '05 #2

P: n/a
On 20 Dec 2004 22:16:03 -0800, sh*******@yahoo.com (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/~a...FAQ-acllc.html
Nov 14 '05 #3

P: n/a
>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
Nov 14 '05 #4

P: n/a
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.
Nov 14 '05 #5

P: n/a
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;
}
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.