Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #109575 > unrolled thread

Printing like palindrome

Started byarnuld <sunrise@invalid.address>
First post2017-05-09 10:59 +0000
Last post2017-05-12 02:32 -0700
Articles 20 on this page of 28 — 13 participants

Back to article view | Back to comp.lang.c


Contents

  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 1 of 2  [1] 2  Next page →


#109575 — Printing like palindrome

Fromarnuld <sunrise@invalid.address>
Date2017-05-09 10:59 +0000
SubjectPrinting like palindrome
Message-ID<5911a0f4$0$294$14726298@news.sunsite.dk>
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 ==================
[arnuld@arch64 programs]$ gcc -ansi -pedantic -Wall -Wextra palindrome.c 
[arnuld@arch64 programs]$ 

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

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

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

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

[toc] | [next] | [standalone]


#109577

FromBarry Schwarz <schwarzb@dqel.com>
Date2017-05-09 04:26 -0700
Message-ID<p093hc1ll75lunca1bsh8k9ahd7efpvduq@4ax.com>
In reply to#109575
On 09 May 2017 10:59:00 GMT, arnuld <sunrise@invalid.address> wrote:

>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");

It would be nice if you told the user what the argument should be.

>  }
>  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?

>    }
>  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.

>  /* 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.

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

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

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#109617

FromRobert Wessel <robertwessel2@yahoo.com>
Date2017-05-09 14:10 -0500
Message-ID<tl44hchl4uroj983a5pbvd0sseovn611dg@4ax.com>
In reply to#109577
On Tue, 09 May 2017 04:26:17 -0700, Barry Schwarz <schwarzb@dqel.com>
wrote:

>On 09 May 2017 10:59:00 GMT, arnuld <sunrise@invalid.address> wrote:
>
>>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");
>
>It would be nice if you told the user what the argument should be.
>
>>  }
>>  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?
>
>>    }
>>  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.
>
>>  /* 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.
>
>>    *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.
>
>>  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.

[toc] | [prev] | [next] | [standalone]


#109620

FromKeith Thompson <kst-u@mib.org>
Date2017-05-09 12:33 -0700
Message-ID<lntw4t965w.fsf@kst-u.example.com>
In reply to#109617
Robert Wessel <robertwessel2@yahoo.com> writes:
[...]
> 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.

> 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-u@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"

[toc] | [prev] | [next] | [standalone]


#109636

FromRichard Heathfield <rjh@cpax.org.uk>
Date2017-05-09 23:05 +0100
Message-ID<oete8h$s9f$1@dont-email.me>
In reply to#109620
On 09/05/17 20:33, Keith Thompson wrote:

<snip>

> you can define an array:
>     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.

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

> 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

[toc] | [prev] | [next] | [standalone]


#109650

Fromraltbos@xs4all.nl (Richard Bos)
Date2017-05-10 10:25 +0000
Message-ID<5912ea6c.1920859@news.xs4all.nl>
In reply to#109636
Richard Heathfield <rjh@cpax.org.uk> wrote:

> On 09/05/17 20:33, Keith Thompson wrote:
> 
> > 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

[toc] | [prev] | [next] | [standalone]


#109652

FromRichard Heathfield <rjh@cpax.org.uk>
Date2017-05-10 11:44 +0100
Message-ID<oeuqov$ccj$1@dont-email.me>
In reply to#109650
On 10/05/17 11:25, Richard Bos wrote:
> Richard Heathfield <rjh@cpax.org.uk> wrote:
>
>> On 09/05/17 20:33, Keith Thompson wrote:
>>
>>> 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

[toc] | [prev] | [next] | [standalone]


#109689

FromKeith Thompson <kst-u@mib.org>
Date2017-05-10 09:48 -0700
Message-ID<lnefvw8xon.fsf@kst-u.example.com>
In reply to#109636
Richard Heathfield <rjh@cpax.org.uk> writes:
> On 09/05/17 20:33, Keith Thompson wrote:
[...]
>> 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-u@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"

[toc] | [prev] | [next] | [standalone]


#109697

FromGareth Owen <gwowen@gmail.com>
Date2017-05-10 18:59 +0100
Message-ID<8737cc1tkd.fsf@gmail.com>
In reply to#109689
Keith Thompson <kst-u@mib.org> writes:

> Richard Heathfield <rjh@cpax.org.uk> writes:
>> On 09/05/17 20:33, Keith Thompson wrote:
> [...]
>>> 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.

[toc] | [prev] | [next] | [standalone]


#109803

Fromarnuld <sunrise@invalid.address>
Date2017-05-12 07:14 +0000
Message-ID<591560cb$0$298$14726298@news.sunsite.dk>
In reply to#109620
> On Tue, 09 May 2017 12:33:31 -0700, Keith Thompson wrote:

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

Good point. Will add the assumption in comments.

[toc] | [prev] | [next] | [standalone]


#109805

Fromarnuld <sunrise@invalid.address>
Date2017-05-12 07:44 +0000
Message-ID<591567c7$0$307$14726298@news.sunsite.dk>
In reply to#109617
> On Tue, 09 May 2017 14:10:38 -0500, Robert Wessel wrote:

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

[toc] | [prev] | [next] | [standalone]


#109804

Fromarnuld <sunrise@invalid.address>
Date2017-05-12 07:20 +0000
Message-ID<59156253$0$298$14726298@news.sunsite.dk>
In reply to#109577
> On Tue, 09 May 2017 04:26:17 -0700, Barry Schwarz wrote:
>> On 09 May 2017 10:59:00 GMT, arnuld <sunrise@invalid.address> wrote:
 

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


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


 
> 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 

[toc] | [prev] | [next] | [standalone]


#109843

FromKeith Thompson <kst-u@mib.org>
Date2017-05-12 08:49 -0700
Message-ID<lnziei5b3w.fsf@kst-u.example.com>
In reply to#109804
arnuld <sunrise@invalid.address> writes:
> On Tue, 09 May 2017 04:26:17 -0700, Barry Schwarz wrote:
[...]
>> 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-u@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"

[toc] | [prev] | [next] | [standalone]


#109926

Fromarnuld <sunrise@invalid.address>
Date2017-05-14 02:01 +0000
Message-ID<5917ba70$0$302$14726298@news.sunsite.dk>
In reply to#109843
> On Fri, 12 May 2017 08:49:23 -0700, Keith Thompson wrote:
> ...SNIP ...
> 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).

I use " gcc -ansi -pedantic -Wall -Wextra palindrome.c"  & you are 
actually right at both points. 

[toc] | [prev] | [next] | [standalone]


#109592

Frommark.bluemel@gmail.com
Date2017-05-09 07:13 -0700
Message-ID<5a1f955b-6510-4632-89c8-cc3e23b39fa8@googlegroups.com>
In reply to#109575
On Tuesday, 9 May 2017 11:58:57 UTC+1, arnuld  wrote:
> Programs works fine, I just wanted suggestions for improvement:

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

[toc] | [prev] | [next] | [standalone]


#109596

Fromscott@slp53.sl.home (Scott Lurndal)
Date2017-05-09 14:59 +0000
Message-ID<cPkQA.151062$3%2.136252@fx09.iad>
In reply to#109592
mark.bluemel@gmail.com writes:
>On Tuesday, 9 May 2017 11:58:57 UTC+1, arnuld  wrote:
>> Programs works fine, I just wanted suggestions for improvement:
>
>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.

[toc] | [prev] | [next] | [standalone]


#109599

FromKeith Thompson <kst-u@mib.org>
Date2017-05-09 08:27 -0700
Message-ID<ln7f1q9hjv.fsf@kst-u.example.com>
In reply to#109596
scott@slp53.sl.home (Scott Lurndal) writes:
> mark.bluemel@gmail.com writes:
>>On Tuesday, 9 May 2017 11:58:57 UTC+1, arnuld  wrote:
>>> Programs works fine, I just wanted suggestions for improvement:
>>
>>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-u@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"

[toc] | [prev] | [next] | [standalone]


#109610

FromBarry Schwarz <schwarzb@dqel.com>
Date2017-05-09 10:40 -0700
Message-ID<7iv3hchfogtk4ddh14putg6f4uneokn4r4@4ax.com>
In reply to#109596
On Tue, 09 May 2017 14:59:20 GMT, scott@slp53.sl.home (Scott Lurndal)
wrote:

>mark.bluemel@gmail.com writes:
>>On Tuesday, 9 May 2017 11:58:57 UTC+1, arnuld  wrote:
>>> Programs works fine, I just wanted suggestions for improvement:
>>
>>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.

>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

[toc] | [prev] | [next] | [standalone]


#109594

Fromscott@slp53.sl.home (Scott Lurndal)
Date2017-05-09 14:38 +0000
Message-ID<gvkQA.151059$3%2.68829@fx09.iad>
In reply to#109575
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;
}

$ cc --std=c99 -o /tmp/a /tmp/a.c
$ /tmp/a N                       
ABCDEFGHIJKLMNMLKJIHGFEDCBA

>
>#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 ==================
>[arnuld@arch64 programs]$ gcc -ansi -pedantic -Wall -Wextra palindrome.c 
>[arnuld@arch64 programs]$ 
>
>[arnuld@arch64 programs]$ ./a.out BA
>A, B, A
>[arnuld@arch64 programs]$ ./a.out D
>A, B, C, D, C, B, A
>
>[arnuld@arch64 programs]$ ./a.out 0
>You did not enter a character. Enter uppercase character 
>
>[arnuld@arch64 programs]$ ./a.out 0*
>You did not enter a character. Enter uppercase character 
>
>[arnuld@arch64 programs]$ ./a.out {
>You did not enter a character. Enter uppercase character 
>[arnuld@arch64 programs]$ 
>
>

[toc] | [prev] | [next] | [standalone]


#109635

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2017-05-09 22:48 +0100
Message-ID<87mvalaei4.fsf@bsb.me.uk>
In reply to#109594
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.

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web