/* C program: atoi function custom Description: atoi function convert string to integer representation of integral numbers. Example 1: string "02302" converts to number 2302 Example 2: string "abcd" returns 0 Example 3: string "-0234" converts to number -234 Example 4: string "0023ABCD" converts to number 23 Example 5: string "12abc45g" converts to number 12 Example 6: string "--0123" returns 0 Example 7: string "" (NULL) returns 0 Example 8: string " 234a" converts to number 234 Example 9: string "+ 239b" returns 0 Example10: string "+239" converts to number 239 Example11: string " +12" converts to number 12 Example12: string " -12" converts to number -12 Compiler: Turbo C++ 3.0 Author: Mangilal Sharma Date: September 12, 2015 Program for: world-of-c-programming.blogpost.com Program number: 102 */ #include<stdio.h> //for printf and gets #include<conio.h> //for clrscr and getch #include<string.h> //for strcpy typedef enum {false, true} bool; //To use bool data type bool isNotNumericChar(char x) //to check the character { return(x >= '0' && x<= '9')? false:true; } int myAtoi(char *str) //Cutom atoi function code { int result = 0; //Initialize result variable int sign = 1; //Initialize sign as positive int i = 0; //index of first digit in string if(str == NULL) return 0; //If number is negative, then update sign while(str[i] == ' ') i++; if(str[i] == '-') { sign = -1; i++; } else if(str[i] == '+') { i++; } //Iterate through all digits of input string and update result value for(;str[i]!= '\0';i++) { if(isNotNumericChar(str[i])) break; //Break when non numeric char occurs //We can also use isdigit function from ctype.h instead of our custom function result = result*10 + str[i] - '0'; //subtracted ASCII value of zero from ASCII value of current digit character } return sign*result; } int main() { char string[] = "02302"; int val = myAtoi(string); clrscr(); printf("String value = %s, Int value = %d\n",string,val); strcpy(string,"C Program"); val = myAtoi(string); printf("String value = %s, Int value = %d\n",string,val); printf("Press y for try your strings to atoi function.\n"); printf("Press any other key to exit.\n"); while(getch() == 'y') { printf("Enter String and press enter:"); gets(string); printf("String value = %s, Int value = %d\n",string,myAtoi(string)); } return 1; }
This is a try to put the solutions of each possible problem related to C and C++. You can find here C basic lab, C++ basic Lab, Data Structure Lab, DAA Lab, Operating System Lab, Graphics Lab, Compiler Lab, Network Lab, and other problems.
Saturday, 12 September 2015
C program for User defined Custom atoi Function Code
Subscribe to:
Post Comments (Atom)
Assembler
ReplyDeleteMemory Address Map
Design: Two-pass Macro Preprocessor
Stack and Subroutines
COCOMO Model (Empirical Process Model)
Dynamic Storage Allocation Techniques
Free Study
ReplyDeletetcp ip reference model
Peephole Optimization
software hierarchy
bully algorithm
rad model
security of digital signature
variable proportions law assumptions
different loading schemes
Really nice blog, very infromative. You can refer more topics related to C language like C pointers and C Programs from here for future articles
ReplyDelete