Discussion:
Printing like palindrome
(too old to reply)
arnuld
2017-05-09 10:59:00 UTC
Permalink
Raw Message
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
*
*/

#include <stdio.h>

void printPalindrome(char c) ;
int getFirstLetter(char* p, char* pc);


int main(int argc, char* argv[]){
char c;
if(2 != argc) {
printf("Please provide 1 argument\n");
}
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;
}


void printPalindrome(char c) {
char t;

for(t='A'; t != c; ++t) printf("%c, ", t);
for(; 'A' != t; --t) printf("%c, ", t);
printf("%c\n", t);
}

================ output ==================
[***@arch64 programs]$ gcc -ansi -pedantic -Wall -Wextra palindrome.c
[***@arch64 programs]$

[***@arch64 programs]$ ./a.out BA
A, B, A
[***@arch64 programs]$ ./a.out D
A, B, C, D, C, B, A

[***@arch64 programs]$ ./a.out 0
You did not enter a character. Enter uppercase character

[***@arch64 programs]$ ./a.out 0*
You did not enter a character. Enter uppercase character

[***@arch64 programs]$ ./a.out {
You did not enter a character. Enter uppercase character
[***@arch64 programs]$
Barry Schwarz
2017-05-09 11:26:17 UTC
Permalink
Raw Message
Post by arnuld
/* GlobalEdx interview Question, Real Life Practicial Code
*
* 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
*
*/
#include <stdio.h>
void printPalindrome(char c) ;
int getFirstLetter(char* p, char* pc);
int main(int argc, char* argv[]){
char c;
if(2 != argc) {
printf("Please provide 1 argument\n");
It would be nice if you told the user what the argument should be.
Post by arnuld
}
else
{
if(!getFirstLetter(argv[1], &c)) printPalindrome(c);
else printf("You did not enter a character. Enter uppercase
character \n");
First half of message is not necessarily correct. What if user
entered q?
Post by arnuld
}
return 0;
}
/* If you get more than letter. Just pick up the first letter
*
*/
int getFirstLetter(char* p, char* pc) {
char temp = *p;
What purpose is served by temp? You could use *p everywhere temp is
coded without the need to create a local variable.
Post by arnuld
/* What if user puts an int or double, here we handle that ? */
if( ((temp >= 'A') && (temp <= 'Z')) ) {
Why all the extra parentheses? Only one set is required. Even if you
parenthesize the operands of && for reading clarity, there is still
one extra set.

On some systems, there are non-alpha characters interspersed between
'A' and 'Z'. Using the standard function isupper() allows your code
to be portable.
Post by arnuld
*pc = temp;
return 0;
}
else return 1;
}
void printPalindrome(char c) {
char t;
for(t='A'; t != c; ++t) printf("%c, ", t);
It would be nice if you were consistent in your use of horizontal
white space. Why is = squished but not !=?

Testing isupper() before printing allows your code to handle the
situation described above.
Post by arnuld
for(; 'A' != t; --t) printf("%c, ", t);
printf("%c\n", t);
}
--
Remove del for email
Robert Wessel
2017-05-09 19:10:38 UTC
Permalink
Raw Message
Post by Barry Schwarz
Post by arnuld
/* GlobalEdx interview Question, Real Life Practicial Code
*
* 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
*
*/
#include <stdio.h>
void printPalindrome(char c) ;
int getFirstLetter(char* p, char* pc);
int main(int argc, char* argv[]){
char c;
if(2 != argc) {
printf("Please provide 1 argument\n");
It would be nice if you told the user what the argument should be.
Post by arnuld
}
else
{
if(!getFirstLetter(argv[1], &c)) printPalindrome(c);
else printf("You did not enter a character. Enter uppercase
character \n");
First half of message is not necessarily correct. What if user
entered q?
Post by arnuld
}
return 0;
}
/* If you get more than letter. Just pick up the first letter
*
*/
int getFirstLetter(char* p, char* pc) {
char temp = *p;
What purpose is served by temp? You could use *p everywhere temp is
coded without the need to create a local variable.
Post by arnuld
/* What if user puts an int or double, here we handle that ? */
if( ((temp >= 'A') && (temp <= 'Z')) ) {
Why all the extra parentheses? Only one set is required. Even if you
parenthesize the operands of && for reading clarity, there is still
one extra set.
On some systems, there are non-alpha characters interspersed between
'A' and 'Z'. Using the standard function isupper() allows your code
to be portable.
Post by arnuld
*pc = temp;
return 0;
}
else return 1;
}
void printPalindrome(char c) {
char t;
for(t='A'; t != c; ++t) printf("%c, ", t);
It would be nice if you were consistent in your use of horizontal
white space. Why is = squished but not !=?
Testing isupper() before printing allows your code to handle the
situation described above.
Post by arnuld
for(; 'A' != t; --t) printf("%c, ", t);
printf("%c\n", t);
}
Is that sufficient? I know C doesn't require A..Z to be contiguous,
but does it require the alphabet to be in ascending order in the
character set? I'm not aware that it does (the requirements for the
digits to be ascending and contiguous is quite explicit and specific).

I know of character sets where the letters are not in the conventional
order (that various Baudot codes, for example), but I don't know of
any implementations of C that actually use such.
Keith Thompson
2017-05-09 19:33:31 UTC
Permalink
Raw Message
Robert Wessel <***@yahoo.com> writes:
[...]
Post by Robert Wessel
Is that sufficient? I know C doesn't require A..Z to be contiguous,
but does it require the alphabet to be in ascending order in the
character set? I'm not aware that it does (the requirements for the
digits to be ascending and contiguous is quite explicit and specific).
It doesn't. Both EBCDIC and the various ASCII-based character sets do
have the letters in order, but there's no such requirement.
Post by Robert Wessel
I know of character sets where the letters are not in the conventional
order (that various Baudot codes, for example), but I don't know of
any implementations of C that actually use such.
Nor do I.

But if you want to write code that doesn't make any such assumptions,
you can define an array:
const char upper[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
and iterate over it.

Of course you *can* just assume that the letters are contiguous and
ordered, but if you do I suggest clearly documenting the assumption,
and perhaps even checking it ('Z' - 'A' == 25 isn't a perfect test,
but it's something).

You might also think about non-Latin letters.
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Richard Heathfield
2017-05-09 22:05:10 UTC
Permalink
Raw Message
On 09/05/17 20:33, Keith Thompson wrote:

<snip>
Post by Keith Thompson
const char upper[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
and iterate over it.
This is the solution I would have proposed if you hadn't beaten me to it.
Post by Keith Thompson
Of course you *can* just assume that the letters are contiguous and
ordered, but if you do I suggest clearly documenting the assumption,
and perhaps even checking it ('Z' - 'A' == 25 isn't a perfect test,
but it's something).
It's worse. The iteration will work correctly in all circumstances. The
assumption might not.
Post by Keith Thompson
You might also think about non-Latin letters.
You might, but the spec clearly said English letters, so why look
further afield?
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
Richard Bos
2017-05-10 10:25:39 UTC
Permalink
Raw Message
Post by Richard Heathfield
Post by Keith Thompson
You might also think about non-Latin letters.
You might, but the spec clearly said English letters, so why look
further afield?
But what if you want to iterate over all letters in EMILY BRONTË?

Richard
Richard Heathfield
2017-05-10 10:44:53 UTC
Permalink
Raw Message
Post by Richard Bos
Post by Richard Heathfield
Post by Keith Thompson
You might also think about non-Latin letters.
You might, but the spec clearly said English letters, so why look
further afield?
But what if you want to iterate over all letters in EMILY BRONTË?
The English letters in that name are E M I L Y B R O N and T. The final
letter is not English. In fact, the name BRONTË is an invention by
Emily's father, Patrick Brunty (who was Irish).
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
Keith Thompson
2017-05-10 16:48:56 UTC
Permalink
Raw Message
[...]
Post by Richard Heathfield
Post by Keith Thompson
You might also think about non-Latin letters.
You might, but the spec clearly said English letters, so why look
further afield?
Sure, but there's no harm in thinking about them, even if the result
of that thought is that you don't have to do anything about them.

And specs can change.
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Gareth Owen
2017-05-10 17:59:46 UTC
Permalink
Raw Message
Post by Keith Thompson
[...]
Post by Richard Heathfield
Post by Keith Thompson
You might also think about non-Latin letters.
You might, but the spec clearly said English letters, so why look
further afield?
Sure, but there's no harm in thinking about them, even if the result
of that thought is that you don't have to do anything about them.
And specs can change.
In this case, they'd have to
'List the letters of the alphabet from "a" to "ë"'
is not well defined.
arnuld
2017-05-12 07:14:19 UTC
Permalink
Raw Message
...SNIP ...
But if you want to write code that doesn't make any such assumptions,
const char upper[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
and iterate over it.
Of course you *can* just assume that the letters are contiguous and
ordered, but if you do I suggest clearly documenting the assumption, and
perhaps even checking it ('Z' - 'A' == 25 isn't a perfect test, but it's
something).
Good point. Will add the assumption in comments.
arnuld
2017-05-12 07:44:07 UTC
Permalink
Raw Message
Is that sufficient? I know C doesn't require A..Z to be contiguous, but
does it require the alphabet to be in ascending order in the character
set? I'm not aware that it does (the requirements for the digits to be
ascending and contiguous is quite explicit and specific).
..SNIP ...
Sufficient ? ... Let me explain a bit

After I told the interviewer about how I am gonna write code and wrote
only the printing function (at his request of only writing that part), he
showed the code to someone else and they both laughed at me. I was told
to leave after that.

If I would have known this "non-contiguous" characteristic and would have
told him same then I think he would have not even let me write code but
certify me "crazy" instead. My interpretation is solely based on the
questions he was asking me and his inability to understand my code which
everyone did understand here.
arnuld
2017-05-12 07:20:51 UTC
Permalink
Raw Message
Post by Barry Schwarz
It would be nice if you told the user what the argument should be.
...SNIP ....
First half of message is not necessarily correct. What if user entered
q?
Well it was interview question and the guy was least bothered about the
message. If I would have written "Congrats for voting for Trump", he
would not have cared either.
Post by Barry Schwarz
What purpose is served by temp? You could use *p everywhere temp is
coded without the need to create a local variable.
Yeah I could use *p. Thought it was a little messy for the interviewer to
see how pointers are being used. It would have been difficult for him to
see what's going on. I could conclude that from the kind of questions he
was asking me.
Post by Barry Schwarz
Why all the extra parentheses? Only one set is required. Even if you
parenthesize the operands of && for reading clarity, there is still one
extra set.
GCC complains if I don't put extra set of parenthesis. I did not wantany
warning on my face
Keith Thompson
2017-05-12 15:49:23 UTC
Permalink
Raw Message
[...]
Post by arnuld
Post by Barry Schwarz
Why all the extra parentheses? Only one set is required. Even if you
parenthesize the operands of && for reading clarity, there is still one
extra set.
GCC complains if I don't put extra set of parenthesis. I did not wantany
warning on my face
The original test was:

if( ((temp >= 'A') && (temp <= 'Z')) )

If I change it to:

if (temp >= 'A' && temp <= 'Z') return 1;

I don't get any warnings with "gcc -Wall -Wextra" (and I find it
easier to read).
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
arnuld
2017-05-14 02:01:20 UTC
Permalink
Raw Message
...SNIP ...
if (temp >= 'A' && temp <= 'Z') return 1;
I don't get any warnings with "gcc -Wall -Wextra" (and I find it easier
to read).
I use " gcc -ansi -pedantic -Wall -Wextra palindrome.c" & you are
actually right at both points.

m***@gmail.com
2017-05-09 14:13:54 UTC
Permalink
Raw Message
As Barry points out, it wouldn't run fine everywhere.

All the world is not ASCII.

See <https://en.wikipedia.org/wiki/EBCDIC> and note what comes
after 'i' for example...
Scott Lurndal
2017-05-09 14:59:20 UTC
Permalink
Raw Message
Post by m***@gmail.com
As Barry points out, it wouldn't run fine everywhere.
All the world is not ASCII.
See <https://en.wikipedia.org/wiki/EBCDIC> and note what comes
after 'i' for example...
Although IBM Z-series and Unisys Clearpath have C compilers, they're not widely used. The
rest of the EBCDIC machines either don't have C or have been
obsolete for a decade or two.

In any case, the problem statement didn't require universal portability.
Keith Thompson
2017-05-09 15:27:32 UTC
Permalink
Raw Message
Post by Scott Lurndal
Post by m***@gmail.com
As Barry points out, it wouldn't run fine everywhere.
All the world is not ASCII.
See <https://en.wikipedia.org/wiki/EBCDIC> and note what comes
after 'i' for example...
Although IBM Z-series and Unisys Clearpath have C compilers, they're
not widely used. The rest of the EBCDIC machines either don't have C
or have been obsolete for a decade or two.
In any case, the problem statement didn't require universal portability.
I'd still use isalpha().

Incidentally, isalpha() has undefined behavior given a negative argument
(other than EOF), and some char values can be negative. You need to
write `isalpha((unsigned char)temp)` rather than just `isalpha(temp)`.
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Barry Schwarz
2017-05-09 17:40:00 UTC
Permalink
Raw Message
Post by Scott Lurndal
Post by m***@gmail.com
As Barry points out, it wouldn't run fine everywhere.
All the world is not ASCII.
See <https://en.wikipedia.org/wiki/EBCDIC> and note what comes
after 'i' for example...
Although IBM Z-series and Unisys Clearpath have C compilers, they're not widely used. The
rest of the EBCDIC machines either don't have C or have been
obsolete for a decade or two.
I don't know Unisys but the C family of compilers for the IBM Z-series
seem fairly popular.
Post by Scott Lurndal
In any case, the problem statement didn't require universal portability.
Perhaps not but it didn't specify ASCII either. Regardless, I was
responding to the OP's request for suggested improvements. Portability
certainly seems to fit in that category.
--
Remove del for email
Scott Lurndal
2017-05-09 14:38:04 UTC
Permalink
Raw Message
Post by arnuld
/* GlobalEdx interview Question, Real Life Practicial Code
*
* 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;
}

$ cc --std=c99 -o /tmp/a /tmp/a.c
$ /tmp/a N
ABCDEFGHIJKLMNMLKJIHGFEDCBA
Post by arnuld
#include <stdio.h>
void printPalindrome(char c) ;
int getFirstLetter(char* p, char* pc);
int main(int argc, char* argv[]){
char c;
if(2 != argc) {
printf("Please provide 1 argument\n");
}
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;
}
void printPalindrome(char c) {
char t;
for(t='A'; t != c; ++t) printf("%c, ", t);
for(; 'A' != t; --t) printf("%c, ", t);
printf("%c\n", t);
}
================ output ==================
A, B, A
A, B, C, D, C, B, A
You did not enter a character. Enter uppercase character
You did not enter a character. Enter uppercase character
You did not enter a character. Enter uppercase character
Ben Bacarisse
2017-05-09 21:48:03 UTC
Permalink
Raw Message
Post by Scott Lurndal
Post by arnuld
/* GlobalEdx interview Question, Real Life Practicial Code
*
* 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
*
*/
$ 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.
Scott Lurndal
2017-05-10 13:21:28 UTC
Permalink
Raw Message
Post by Ben Bacarisse
Post by Scott Lurndal
$ 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.
Ben Bacarisse
2017-05-10 14:45:04 UTC
Permalink
Raw Message
Post by Scott Lurndal
Post by Ben Bacarisse
Post by Scott Lurndal
$ 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.
Barry Schwarz
2017-05-10 17:26:10 UTC
Permalink
Raw Message
Post by Scott Lurndal
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
bartc
2017-05-09 20:28:01 UTC
Permalink
Raw Message
Post by arnuld
/* 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.
Post by arnuld
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 !=.
Post by arnuld
printf("Please provide 1 argument\n");
"Please enter a letter A to Z"
Post by arnuld
}
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
Richard Bos
2017-05-10 10:27:04 UTC
Permalink
Raw Message
Post by bartc
Post by arnuld
/* 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
Chad
2017-05-10 17:39:16 UTC
Permalink
Raw Message
Post by bartc
Post by arnuld
/* 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.
Post by arnuld
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 !=.
Post by arnuld
printf("Please provide 1 argument\n");
"Please enter a letter A to Z"
Post by arnuld
}
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.
bartc
2017-05-10 11:54:39 UTC
Permalink
Raw Message
Post by arnuld
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
a***@gmail.com
2017-05-12 09:32:26 UTC
Permalink
Raw Message
I do not like
void printPalindrome(char c)
But I like
void printPalindrome(int c)
Or
printPalindrome(c){...}
Loading...