How to convert character

R

rahul

rahul said:
  while (input[count] != NULL) {
  The comparison should be with '\0', though NULL will work.
Not to start the purists debate, NULL works but to be precise, it
should be the '\0'. So, point taken.

(input[count] is of type char, yes?)

I don't see how your statement differs from what santosh wrote.>
It does not. I was just agreeing to what he said:)
 
C

CBFalconer

Barry said:
Without the & of course.

True. I looked at this to resolve how that error got in there, and
remembered that originally I defined charset as an array (charset[]
= ...). Then the & was required. At any rate, good pick.
ixnew = (ix + delta) % strlen(theset);
chnew = theset[ixnew];
}
else /* ignore the char */ ;

The sign of delta switches between encode and decode. There is no
portability problem, and the native char set can be anything you
want.
 
K

Keith Thompson

CBFalconer said:
Keith said:
CBFalconer said:
rahul wrote:
... snip ...

It may, but I don't see any algorithm which can do the rotation
for an arbitrary character set. For argument's sake, there may
not be any pattern at all. 'a' may be 90, 'b' 91, then c 190....
and so on and on. Is it really possible to do this without
taking the particular char set into account?

Assuming the char set to be rotated and within is 'a ... z', then
you can do all sorts of things with:

char *theset = "abcdefghijklmnopqrstuvwxyz";
and use:
if (p = strchr(theset, ch)) /* ch is within the set */
ix = p - &theset;
ixnew = (ix + delta) % strlen(theset);
chnew = theset[ixnew];
}
else /* ignore the char */ ;

The sign of delta switches between encode and decode. There is no
portability problem, and the native char set can be anything you
want.

I see a possible performance problem here. For each letter, strchr()
scans, on average, half of the 26-character string (assuming a uniform
distribution). For each non-letter, it scans the entire string to
determine that it's not a letter. Something that should be O(N)
(where N is the number of characters being processed) becomes O(N*M),
where M is the number of letters in the alphabet.

I would worry more about the use of strlen(theset). That value
should be determined once and used.

You're right, I missed that. (Well, we both did.) But no, I wouldn't
worry *more* about it; I'd worry about them both equally.

(An optimizer might replace streln(theset) with 26, but one shouldn't
count on it.)
 
B

Barry Schwarz

Barry said:
Without the & of course.

True. I looked at this to resolve how that error got in there, and
remembered that originally I defined charset as an array (charset[]
= ...). Then the & was required. At any rate, good pick.

Sorry, but even then the & leads to a constraint violation.


Remove del for email
 
C

CBFalconer

Barry said:
CBFalconer said:
Barry said:
rahul wrote:

snip

It may, but I don't see any algorithm which can do the rotation
for an arbitrary character set. For argument's sake, there may
not be any pattern at all. 'a' may be 90, 'b' 91, then c 190....
and so on and on. Is it really possible to do this without
taking the particular char set into account?

Assuming the char set to be rotated and within is 'a ... z', then
you can do all sorts of things with:

char *theset = "abcdefghijklmnopqrstuvwxyz";
and use:
if (p = strchr(theset, ch)) /* ch is within the set */
ix = p - &theset;

Without the & of course.

True. I looked at this to resolve how that error got in there, and
remembered that originally I defined charset as an array (charset[]
= ...). Then the & was required. At any rate, good pick.

Sorry, but even then the & leads to a constraint violation.

Oh? With:

char charset[] = "abcdefghijklmnopqrstuvwxyz";

then the & is needed to get a pointer to charset, i.e. the address.

cp = &charset;

is valid, I believe. Then "ix = p - cp;" is valid. We just don't
need cp. If this goes on I may have to feed it to a compiler and
see what results. :)
 
B

Barry Schwarz

Barry said:
CBFalconer said:
Barry Schwarz wrote:
rahul wrote:

snip

It may, but I don't see any algorithm which can do the rotation
for an arbitrary character set. For argument's sake, there may
not be any pattern at all. 'a' may be 90, 'b' 91, then c 190....
and so on and on. Is it really possible to do this without
taking the particular char set into account?

Assuming the char set to be rotated and within is 'a ... z', then
you can do all sorts of things with:

char *theset = "abcdefghijklmnopqrstuvwxyz";
and use:
if (p = strchr(theset, ch)) /* ch is within the set */
ix = p - &theset;

Without the & of course.

True. I looked at this to resolve how that error got in there, and
remembered that originally I defined charset as an array (charset[]
= ...). Then the & was required. At any rate, good pick.

Sorry, but even then the & leads to a constraint violation.

Oh? With:

char charset[] = "abcdefghijklmnopqrstuvwxyz";

then the & is needed to get a pointer to charset, i.e. the address.

cp = &charset;

The expression &charset has type pointer to array of 27 char. This is
not the same as pointer to char, nor is there an implicit conversion
between the two.

If you drop the &, then in the context of the assignment statement,
charset will be converted to the address of charset[0] with type
pointer to char. Since cp is a char*, that is exactly what is needed.
is valid, I believe. Then "ix = p - cp;" is valid. We just don't
need cp. If this goes on I may have to feed it to a compiler and
see what results. :)


Remove del for email
 
C

CBFalconer

Barry said:
.... snip ...
char charset[] = "abcdefghijklmnopqrstuvwxyz";

then the & is needed to get a pointer to charset, i.e. the address.

cp = &charset;

The expression &charset has type pointer to array of 27 char.
This is not the same as pointer to char, nor is there an implicit
conversion between the two.

If you drop the &, then in the context of the assignment statement,
charset will be converted to the address of charset[0] with type
pointer to char. Since cp is a char*, that is exactly what is
needed.

Alright, that was my sloppy thinking. Thanks for clearing it up.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top