Computer Science, asked by qasimkhan990000, 1 year ago

Write a program that converts a string like "124" to an integer 124.

Answers

Answered by ZOMBIEME
3

To convert string to integer, we first take a string as input from user using gets function. We have to convert this input string into integer. Input string should consist of digits('0' to '9') and minus sign('-') for negative numbers. It may contains some non-numerical characters like alphabet, but as soon as we see any non-numerical character we stop conversion and return converted integer till now.

now , i dont know the exact code but ,

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

   }

}


qasimkhan990000: thanks ......
Similar questions