Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #109635
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Printing like palindrome |
| Date | 2017-05-09 22:48 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87mvalaei4.fsf@bsb.me.uk> (permalink) |
| References | <5911a0f4$0$294$14726298@news.sunsite.dk> <gvkQA.151059$3%2.68829@fx09.iad> |
scott@slp53.sl.home (Scott Lurndal) writes:
> arnuld <sunrise@invalid.address> writes:
>>Programs works fine, I just wanted suggestions for improvement:
>>
>>
>>/* GlobalEdx interview Question, Real Life Practicial Code
>> *
>> * If a user enters D then write a program to print like this:
>> * A B C D C B A
>> * If user Enters J or Z then it should print till J or Z
>> * like above. Program should work for every letter of English
>> *
>> */
>
> Seems unnecessarily complex:
>
> $ cat /tmp/a.c
> #include <ctype.h>
> #include <stdio.h>
> #include <string.h>
>
> int
> main(int argc, const char **argv, const char **envp)
> {
> const char *cp = argv[1];
> if ((argc < 2) || (strlen(cp) > 1) || !isalpha(*cp)) {
> printf("Argument to '%s' must be a single alphabetic character, not '%s'\n",
> argv[0], argv[1]?argv[1]:"<empty>");
> return 1;
> }
>
> for(char c = 'A'; c != toupper(*cp); c++) printf("%c", c);
> for(char c = toupper(*cp); c >= 'A'; --c) printf("%c", c);
> putc('\n', stdout);
> return 0;
> }
There's the nasty issue that toupper takes an int and is only defined
for positive arguments (and EOF) whereas *cp may be negative.
I'd use:
int target = (unsigned char)*argv[1];
and later
for (int c = 'A'; ...)
Interestingly, if you write
if (isupper(c)) putchar(c);
in the body of the loops the program will work with EBCDIC and more.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Printing like palindrome arnuld <sunrise@invalid.address> - 2017-05-09 10:59 +0000
Re: Printing like palindrome Barry Schwarz <schwarzb@dqel.com> - 2017-05-09 04:26 -0700
Re: Printing like palindrome Robert Wessel <robertwessel2@yahoo.com> - 2017-05-09 14:10 -0500
Re: Printing like palindrome Keith Thompson <kst-u@mib.org> - 2017-05-09 12:33 -0700
Re: Printing like palindrome Richard Heathfield <rjh@cpax.org.uk> - 2017-05-09 23:05 +0100
Re: Printing like palindrome raltbos@xs4all.nl (Richard Bos) - 2017-05-10 10:25 +0000
Re: Printing like palindrome Richard Heathfield <rjh@cpax.org.uk> - 2017-05-10 11:44 +0100
Re: Printing like palindrome Keith Thompson <kst-u@mib.org> - 2017-05-10 09:48 -0700
Re: Printing like palindrome Gareth Owen <gwowen@gmail.com> - 2017-05-10 18:59 +0100
Re: Printing like palindrome arnuld <sunrise@invalid.address> - 2017-05-12 07:14 +0000
Re: Printing like palindrome arnuld <sunrise@invalid.address> - 2017-05-12 07:44 +0000
Re: Printing like palindrome arnuld <sunrise@invalid.address> - 2017-05-12 07:20 +0000
Re: Printing like palindrome Keith Thompson <kst-u@mib.org> - 2017-05-12 08:49 -0700
Re: Printing like palindrome arnuld <sunrise@invalid.address> - 2017-05-14 02:01 +0000
Re: Printing like palindrome mark.bluemel@gmail.com - 2017-05-09 07:13 -0700
Re: Printing like palindrome scott@slp53.sl.home (Scott Lurndal) - 2017-05-09 14:59 +0000
Re: Printing like palindrome Keith Thompson <kst-u@mib.org> - 2017-05-09 08:27 -0700
Re: Printing like palindrome Barry Schwarz <schwarzb@dqel.com> - 2017-05-09 10:40 -0700
Re: Printing like palindrome scott@slp53.sl.home (Scott Lurndal) - 2017-05-09 14:38 +0000
Re: Printing like palindrome Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-09 22:48 +0100
Re: Printing like palindrome scott@slp53.sl.home (Scott Lurndal) - 2017-05-10 13:21 +0000
Re: Printing like palindrome Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-10 15:45 +0100
Re: Printing like palindrome Barry Schwarz <schwarzb@dqel.com> - 2017-05-10 10:26 -0700
Re: Printing like palindrome bartc <bc@freeuk.com> - 2017-05-09 21:28 +0100
Re: Printing like palindrome raltbos@xs4all.nl (Richard Bos) - 2017-05-10 10:27 +0000
Re: Printing like palindrome Chad <cdalten@gmail.com> - 2017-05-10 10:39 -0700
Re: Printing like palindrome bartc <bc@freeuk.com> - 2017-05-10 12:54 +0100
Printing like palindrome asetofsymbols@gmail.com - 2017-05-12 02:32 -0700
csiph-web