Publicité
Write a recursive function to convert a character string of digits to.docx
Write a recursive function to convert a character string of digits to.docx
Prochain SlideShare
Write a SIMPLE java program to print out the numbers from 100 to 1- wi.docxWrite a SIMPLE java program to print out the numbers from 100 to 1- wi.docx
Chargement dans ... 3
1 sur 2
Publicité

Contenu connexe

Plus de drosa1(20)

Publicité

Write a recursive function to convert a character string of digits to.docx

  1. Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234―) returns 1234. Hint: To convert a character to a number, subtract the ASCII value ‘0’ from the character. For example, if the string s has only one character, then the function can return the value s[0] – ‘0’. Solution #include<stdio.h> int strToInt(char[] ); int main(){ char str[10]; int Value; printf("Enter any integer as a string: "); scanf("%s",str); Value = strToInt(str); printf("Equivalent integer value: %d",Value); return 0; } int strToInt(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. "); return 0; } else{ sum = sum*10 + (str[i] - 48);
  2. i++; } } return sum; }
Publicité