How to convert string to int without using library functions in c





How to convert string to int without using library functions in c programming language

#include<stdio.h>

int stringToInt(char[] );
int main(){
  
    char str[10];
    int intValue;

    printf("Enter any integer as a string: ");
    scanf("%s",str);

   intValue = stringToInt(str);

    printf("Equivalent integer value: %d",intValue);

    return 0;
}

int stringToInt(char str[]){
    int i=0,sum=0;

    while(str[i]!='\0'){
         if(str[i]< 48 || str[i] > 57){
             printf("Unable to convert it into integer.\n");
             return 0;
         }
         else{
             sum = sum*10 + (str[i] - 48);
             i++;
         }

    }

    return sum;

}

Sample output:
Enter any integer as a string: 123
Equivalent integer value: 123






12 comments:

  1. while(str[i]!='\0'){
    if(str[i]< 48 || str[i] > 57){
    printf("Unable to convert it into integer.\n");
    return 0;
    }
    else{
    sum = sum*10 + (str[i] - 48);
    i++;
    }

    for this part, i do not understand the str[i] is smaller than 48 and larger than 57?
    Can you explain it again?

    ReplyDelete
  2. str[i] will return ASCII value of that character. ASCII value of '0' is 48 and '9' is 57.

    ReplyDelete
  3. can any one write the full c program for day number for example 04-feb-2012 must be printed as 35 2012
    means jan as 31 days so 31+4=35

    ReplyDelete
    Replies
    1. #include
      int main()
      {
      char a[12]={'jan','feb','mar','april','may','june','july','aug','sep','oct','nov','dec'};
      int sum=0,month,day,year,i;
      printf("enter day");
      scanf("%d",&day);
      printf("enter month");
      scanf("%d",&month);
      printf("enter year");
      scanf("%d",&year);
      printf("%d-%d-%d",day,month,year);
      for(i=1;i<=month-1;i++)
      {
      switch(i)
      {
      case 1:if(i==1)
      sum+=31;
      break;
      case 2:if(i==2)
      {
      if(year%4==0)
      sum+=29;
      else
      sum+=28;
      }
      break;
      case 3:if(i==3)
      sum+=31;
      break;
      case 4:if(i==4)
      sum+=30;
      break;
      case 5:if(i==5)
      sum+=31;
      break;
      case 6:if(i==6)
      sum+=30;
      break;
      case 7:if(i==7)
      sum+=31;
      break;
      case 8:if(i==8)
      sum+=31;
      break;
      case 9:if(i==9)
      sum+=30;
      break;
      case 10:if(i==10)
      sum+=31;
      break;
      case 11:if(i==11)
      sum+=30;
      break;
      case 12:if(i==12)
      sum+=31;
      break;
      default :
      break;
      }
      }
      sum+=day;
      printf("sum of days:%d",sum);
      }
      /*OUTPUT:-
      enter day 06
      enter month 04
      enter year 1996
      sum of days:97*/

      Delete
    2. #include
      int main()
      {
      char a[12]={'jan','feb','mar','april','may','june','july','aug','sep','oct','nov','dec'};
      int sum=0,month,day,year,i;
      printf("enter day");
      scanf("%d",&day);
      printf("enter month");
      scanf("%d",&month);
      printf("enter year");
      scanf("%d",&year);
      printf("%d-%d-%d",day,month,year);
      for(i=1;i<=month-1;i++)
      {
      switch(i)
      {
      case 1:if(i==1)
      sum+=31;
      break;
      case 2:if(i==2)
      {
      if(year%4==0)
      sum+=29;
      else
      sum+=28;
      }
      break;
      case 3:if(i==3)
      sum+=31;
      break;
      case 4:if(i==4)
      sum+=30;
      break;
      case 5:if(i==5)
      sum+=31;
      break;
      case 6:if(i==6)
      sum+=30;
      break;
      case 7:if(i==7)
      sum+=31;
      break;
      case 8:if(i==8)
      sum+=31;
      break;
      case 9:if(i==9)
      sum+=30;
      break;
      case 10:if(i==10)
      sum+=31;
      break;
      case 11:if(i==11)
      sum+=30;
      break;
      case 12:if(i==12)
      sum+=31;
      break;
      default :
      break;
      }
      }
      sum+=day;
      printf("sum of days:%d",sum);
      }
      /*OUTPUT:-
      enter day 06
      enter month 04
      enter year 1996
      sum of days:97*/

      Delete
  4. how can i print prime number in if else condition with out using loop;

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. #include
      int prime(int y);
      int main()
      {
      int n,x;
      printf("enter number");
      scanf("%d",&n);
      x=prime(n);
      if(x==1)
      printf("%d is a prime number",n);
      else
      printf("%d is not a prime number",n);
      }
      int prime(int y)
      {
      static int count=0,i=2;
      if(y>=i)
      {
      if(y%i==0)
      count++;
      i++;
      prime(y);
      }
      return(count);
      }
      OUTPUT:-
      enter number 5
      5 is a prime number

      Delete
    3. #include
      int prime(int y);
      int main()
      {
      int n,x;
      printf("enter number");
      scanf("%d",&n);
      x=prime(n);
      if(x==1)
      printf("%d is a prime number",n);
      else
      printf("%d is not a prime number",n);
      }
      int prime(int y)
      {
      static int count=0,i=2;
      if(y>=i)
      {
      if(y%i==0)
      count++;
      i++;
      prime(y);
      }
      return(count);
      }
      OUTPUT:-
      enter number 5
      5 is a prime number

      Delete
  5. may i put
    sum = sum*10 + (str[i] );
    instead of
    sum = sum*10 + (str[i] - 48); ?
    and here 48 means 0?

    ReplyDelete
  6. Space thakle sehulo ashhbe kivabe?

    ReplyDelete