Sunday 16 June 2013

Write A Program That Converts A String...

Another exercise attempt. I almost have a headache after writing this. I'm going out for a drink. When I saw the question, the first thing that came to my mind was a switch case (I'm assuming the author does not want us to use the atoi ASCII to Integer function), so I wrote it using it switch case although there probably are other ways to do this, I can't be bothered now. To increase the number the user can input, I used unsigned integers. If you want the program to accept only smaller numbers:
1) Change unsigned to int if needed
    If you do this also replace %u with a %i in:
    printf("Here is your input as an (Unsigned) integer: %i",integer);
2) Change prompt the "upto 9 digits" part.
3) if(str_length<=9) replace 9 with desired limit.
4) Delete or comment out the code starting from case 9: to right before case:desired limit.

Let Us C, Chapter 9, Exercise D-d:
Write a program that converts a string like "124" to an integer 124.

#include <stdio.h>
#include <strings.h>

main()
{
    unsigned str_length,temp,integer=0;
    int i=0;
    char input[9];

    printf("Enter a string of numbers (upto 9 digits): ");
    scanf(" %s",input);

    str_length=strlen(input);
    if(str_length<=9)
    {
        switch(str_length)
        {
        case 9:     //if it is a 9 digit number
            /*Without the -48, temp will be assigned the ASCII value, 48 for 0, 49 for 1, 50 for 2 etc.
            so we subtract 48 to cut it down to the correct value*/
            temp=input[i]-48;
            integer=temp*100000000;
            i++;

        case 8:
            temp=input[i]-48;
            integer=temp*10000000+integer;
            i++;

        case 7:
            temp=input[i]-48;
            integer=temp*1000000+integer;
            i++;

        case 6:
            temp=input[i]-48;
            integer=temp*100000+integer;
            i++;

        case 5:
            temp=input[i]-48;
            integer=(temp*10000)+integer;
            i++;

        case 4:
            temp=input[i]-48;
            integer=(temp*1000)+integer;
            i++;

        case 3:
            temp=input[i]-48;
            integer=(temp*100)+integer;
            i++;

        case 2:
            temp=input[i]-48;
            integer=(temp*10)+integer;
            i++;

        case 1:
            temp=input[i]-48;
            integer=temp+integer;
            i++;
        }
        printf("Here is your input as an (Unsigned) integer: %u",integer);
    }

    else
    {
        printf("Invalid Input!");
    }

}

5 comments:

  1. I didnt understand the use of
    Temp=input[i]-48
    Integer=temp+integer

    ReplyDelete
  2. I didnt understand the use of
    Temp=input[i]-48
    Integer=temp+integer

    ReplyDelete
    Replies
    1. Basically, we are figuring out every number and its unit.
      Say the user entered 987654321. We are breaking it down to
      900000000 + 80000000 + 7000000 + 600000 + 50000 + 4000 + 300 + 20 + 1
      So, if we did not add the last temp (a one in this case), as we did with the last
      Temp=input[i]-48
      Integer=temp+integer
      the result would be displayed as 987654320 (last digit is a zero not one)

      Have a good day!

      Delete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Writing 'if' makes the switch to execute only once which won't give the correct answer.
    We have to use any one of the looping such as while or for,so that we iterate required no of times by incrementing the variable 'i'.
    For example,if string="124" then we have to iterate 3 times to execute 3 different cases(case 3:,case 2:,case 1:) in order in 3 different iterations as 3 cases can't be executed at the same time in a switch.

    ReplyDelete