Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #109575 > unrolled thread
| Started by | arnuld <sunrise@invalid.address> |
|---|---|
| First post | 2017-05-09 10:59 +0000 |
| Last post | 2017-05-12 02:32 -0700 |
| Articles | 8 on this page of 28 — 13 participants |
Back to article view | Back to comp.lang.c
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
Page 2 of 2 — ← Prev page 1 [2]
| From | scott@slp53.sl.home (Scott Lurndal) |
|---|---|
| Date | 2017-05-10 13:21 +0000 |
| Message-ID | <stEQA.5449$Pq7.306@fx21.iad> |
| In reply to | #109635 |
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>scott@slp53.sl.home (Scott Lurndal) writes:
>
>> $ 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.
For EBCDIC, yes all the alpha characters have the high-order bit
set. For ASCII the values that pass the validity checks will not
be greater than 0x7f.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2017-05-10 15:45 +0100 |
| Message-ID | <8760h893f3.fsf@bsb.me.uk> |
| In reply to | #109663 |
scott@slp53.sl.home (Scott Lurndal) writes:
> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>scott@slp53.sl.home (Scott Lurndal) writes:
>>
>
>>> $ 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.
>
> For EBCDIC, yes all the alpha characters have the high-order bit
> set. For ASCII the values that pass the validity checks will not
> be greater than 0x7f.
But the same applies to the validity check. You can't be sure that
isalpha(*cp) is not being passed a negative value.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2017-05-10 10:26 -0700 |
| Message-ID | <l6j6hc1tnjnqoqri7oopngmdd480tfv7np@4ax.com> |
| In reply to | #109663 |
On Wed, 10 May 2017 13:21:28 GMT, scott@slp53.sl.home (Scott Lurndal) wrote: >For EBCDIC, yes all the alpha characters have the high-order bit >set. For ASCII the values that pass the validity checks will not >be greater than 0x7f. On C compilers for EBCDIC systems, char defaults to unsigned char so the high order bit is not a sign bit. -- Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | bartc <bc@freeuk.com> |
|---|---|
| Date | 2017-05-09 21:28 +0100 |
| Message-ID | <cDpQA.18290$EF.7869@fx42.am4> |
| In reply to | #109575 |
On 09/05/2017 11:59, arnuld wrote:
> Programs works fine, I just wanted suggestions for improvement:
>
>
> /* GlobalEdx interview Question, Real Life Practicial Code
> void printPalindrome(char c) ;
If it's for an interview, then I wouldn't call it a palindrome, because
it isn't.
A palindrome is a word or phrase (or sometimes number) which spells out
the same thing when the character sequence is reversed (usually ignoring
spacing, punctuation and capitalisation).
This just prints a letter sequence, then in reverse, avoiding
duplicating the letter in the middle.
> int getFirstLetter(char* p, char* pc);
>
>
> int main(int argc, char* argv[]){
> char c;
> if(2 != argc) {
Ugly. And the idiom is mainly intended for == not !=.
> printf("Please provide 1 argument\n");
"Please enter a letter A to Z"
> }
> else
> {
> if(!getFirstLetter(argv[1], &c)) printPalindrome(c);
> else printf("You did not enter a character. Enter uppercase
> character \n");
> }
> return 0;
> }
>
> /* If you get more than letter. Just pick up the first letter
> *
> */
> int getFirstLetter(char* p, char* pc) {
> char temp = *p;
>
> /* What if user puts an int or double, here we handle that ? */
> if( ((temp >= 'A') && (temp <= 'Z')) ) {
> *pc = temp;
> return 0;
> }
> else return 1;
> }
The program seems to work, but you spend a disproportionate amount of
code verifying the input, which is just a letter from A to Z. And it's
not verifying it too well as anything is accepted not just a single
letter. (It doesn't work for lower case either, but that is slightly
trickier.)
(Also, if you run it on Linux, and enter * or ?, it'll use the name of
the first matching file encountered. But you can't do much about that.)
--
bartc
[toc] | [prev] | [next] | [standalone]
| From | raltbos@xs4all.nl (Richard Bos) |
|---|---|
| Date | 2017-05-10 10:27 +0000 |
| Message-ID | <5912eabd.2002234@news.xs4all.nl> |
| In reply to | #109629 |
bartc <bc@freeuk.com> wrote: > On 09/05/2017 11:59, arnuld wrote: > > Programs works fine, I just wanted suggestions for improvement: > > > > /* GlobalEdx interview Question, Real Life Practicial Code > > > void printPalindrome(char c) ; > > If it's for an interview, then I wouldn't call it a palindrome, because > it isn't. > > A palindrome is a word or phrase (or sometimes number) which spells out > the same thing when the character sequence is reversed (usually ignoring > spacing, punctuation and capitalisation). > > This just prints a letter sequence, then in reverse, avoiding > duplicating the letter in the middle. Which is /ipso facto/ a palindrome. A meaningless one, but it is one. Richard
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2017-05-10 10:39 -0700 |
| Message-ID | <ef6bfda5-e88c-4291-a290-59db10b50a0d@googlegroups.com> |
| In reply to | #109629 |
On Tuesday, May 9, 2017 at 1:27:59 PM UTC-7, Bart wrote:
> On 09/05/2017 11:59, arnuld wrote:
> > Programs works fine, I just wanted suggestions for improvement:
> >
> >
> > /* GlobalEdx interview Question, Real Life Practicial Code
>
> > void printPalindrome(char c) ;
>
> If it's for an interview, then I wouldn't call it a palindrome, because
> it isn't.
>
> A palindrome is a word or phrase (or sometimes number) which spells out
> the same thing when the character sequence is reversed (usually ignoring
> spacing, punctuation and capitalisation).
>
> This just prints a letter sequence, then in reverse, avoiding
> duplicating the letter in the middle.
>
> > int getFirstLetter(char* p, char* pc);
> >
> >
> > int main(int argc, char* argv[]){
> > char c;
> > if(2 != argc) {
>
> Ugly. And the idiom is mainly intended for == not !=.
>
> > printf("Please provide 1 argument\n");
>
> "Please enter a letter A to Z"
>
> > }
> > else
> > {
> > if(!getFirstLetter(argv[1], &c)) printPalindrome(c);
> > else printf("You did not enter a character. Enter uppercase
> > character \n");
> > }
> > return 0;
> > }
> >
> > /* If you get more than letter. Just pick up the first letter
> > *
> > */
> > int getFirstLetter(char* p, char* pc) {
> > char temp = *p;
> >
> > /* What if user puts an int or double, here we handle that ? */
> > if( ((temp >= 'A') && (temp <= 'Z')) ) {
> > *pc = temp;
> > return 0;
> > }
> > else return 1;
> > }
>
> The program seems to work, but you spend a disproportionate amount of
> code verifying the input, which is just a letter from A to Z. And it's
> not verifying it too well as anything is accepted not just a single
> letter. (It doesn't work for lower case either, but that is slightly
> trickier.)
>
> (Also, if you run it on Linux, and enter * or ?, it'll use the name of
> the first matching file encountered. But you can't do much about that.)
>
> --
For whatever reasons, I've followed this braindead crap. Anyways, I have a hard time believing that these are actual interview questions. This is because, at least in my limited experience, every major software firm that I've interviewed with asked programming questions that were far far more difficult. In some cases, C wasn't always the correct way to solve the problem. Ie, I had to break out the Erlang.
So either this fool is making things up or he is interviewing for a tech position at a software firm located in someone's mothers basement?
Who knows. I could be wrong.
[toc] | [prev] | [next] | [standalone]
| From | bartc <bc@freeuk.com> |
|---|---|
| Date | 2017-05-10 12:54 +0100 |
| Message-ID | <UbDQA.5349$R8.985@fx25.am4> |
| In reply to | #109575 |
On 09/05/2017 11:59, arnuld wrote:
> if(!getFirstLetter(argv[1], &c)) printPalindrome(c);
> else printf("You did not enter a character. Enter uppercase
> character \n");
> }
> return 0;
> }
I didn't look at the actual code too closely before. This looks untidy.
It seems that getFirstLetter() returns a status 0 for success, and 1 for
failure, with the letter itself via a pointer. But that's not so obvious
from looking at this. It's also overkill.
I think better would be:
if (c = getFirstLetter(argv[1]))
printPalindrome(c);
else
... deal with error...
With c == 0 signifying an error.
(And since the checking done by getFirstLetter is perfunctory, the
following would do:
c = *argv[1];
if (c >= 'A' && c <= 'Z') // or any of a million other ways
printPalindrome(c);
Personally I would also arrange it to get the verification out of the
way first, then I have my verified input c, and can do what I need to do
with it without still being embroiled in user interface stuff.)
--
bartc
[toc] | [prev] | [next] | [standalone]
| From | asetofsymbols@gmail.com |
|---|---|
| Date | 2017-05-12 02:32 -0700 |
| Message-ID | <924bd935-d111-4402-b68b-9ea42b5d3d20@googlegroups.com> |
| In reply to | #109575 |
I do not like
void printPalindrome(char c)
But I like
void printPalindrome(int c)
Or
printPalindrome(c){...}
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c
csiph-web