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

D

Durgesh Sharma

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
 
A

Arthur J. O'Dwyer

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.)
 
J

Jack Klein

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*
 
G

Gordon Burditt

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
 
I

infobahn

Durgesh said:
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.
 
E

E. Robert Tisdale

Durgesh said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top