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


Groups > comp.lang.c > #69841

Re: Issues with array, strlen, etc

From bartekltg <bartekltg@gmail.com>
Newsgroups comp.lang.c
Subject Re: Issues with array, strlen, etc
Date 2015-09-07 22:03 +0200
Organization ATMAN - ATM S.A.
Message-ID <mskqii$4d1$1@node1.news.atman.pl> (permalink)
References <1866812f-e415-44b4-8127-8e21c4a13653@googlegroups.com>

Show all headers | View raw


On 07.09.2015 19:47, Alla _ wrote:
> Hello!
>
> I have started working on a new task.
> I decided to break the program into
> parts and then combine them.
>
> First part should prompt the user for
> a card number; then multiply every other
> digit by 2, starting from the number's
> second-to-last digit. (please, bear with
> me until I will be able to get rid of
> scanf; for now, please, allow me to
> use it).
>

>
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
>      char card_number[16];
>
> /** limiting the number of possible inputs by
>      16, because it represents the longest card
>      number
> **/
>      int i, digit;
>
>      printf ("Please, enter your card number:  \n");
>
>      if (scanf ("%16s", card_number) == 1)
>      {
>          for (i = (strlen(card_number)-1); i >= 0; i -= 1)
>          {
>              digit = card_number[i] * 2;

card_number[i] is an ASCII code for a character.
https://en.wikipedia.org/wiki/ASCII#ASCII_printable_code_chart
character '0' is represented as _48_.

You have to decode ascii first.
One way is to subtract a code for zero

digit = (card_number[i]-'0') * 2;

Do you know why this works?

>              printf ("%i", digit);

6 times 2 is 12, this is not a digit;-)
The output still looks like gibberish, add space

         printf ("%i ", digit);

bartekltg

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-07 10:47 -0700
  Re: Issues with array, strlen, etc James Kuyper <jameskuyper@verizon.net> - 2015-09-07 14:47 -0400
  Re: Issues with array, strlen, etc bartekltg <bartekltg@gmail.com> - 2015-09-07 22:03 +0200
  Re: Issues with array, strlen, etc Paul N <gw7rib@aol.com> - 2015-09-07 13:08 -0700
  Re: Issues with array, strlen, etc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-07 22:55 +0100
    Re: Issues with array, strlen, etc Rosario19 <Ros@invalid.invalid> - 2015-09-08 07:59 +0200
      Re: Issues with array, strlen, etc Rosario19 <Ros@invalid.invalid> - 2015-09-08 08:05 +0200
  Re: Issues with array, strlen, etc Barry Schwarz <schwarzb@dqel.com> - 2015-09-07 20:35 -0700
  Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-08 01:08 -0700
    Re: Issues with array, strlen, etc Keith Thompson <kst-u@mib.org> - 2015-09-08 09:04 -0700
      Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-10 01:22 -0700
        Re: Issues with array, strlen, etc Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 09:38 +0100
          Re: Issues with array, strlen, etc Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-09-10 07:34 -0400
            Re: Issues with array, strlen, etc David Brown <david.brown@hesbynett.no> - 2015-09-10 13:47 +0200
              Re: Issues with array, strlen, etc Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 13:11 +0100
          Re: Issues with array, strlen, etc Keith Thompson <kst-u@mib.org> - 2015-09-10 10:21 -0700
        Re: Issues with array, strlen, etc Geoff <geoff@invalid.invalid> - 2015-09-10 10:42 -0700
  Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-08 01:11 -0700
    Re: Issues with array, strlen, etc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 09:51 +0100
      Re: Issues with array, strlen, etc Alla _ <modelling.data@gmail.com> - 2015-09-08 02:39 -0700

csiph-web