/**************************************************** * CP-I Warmup 1999-2000 * Problem #6 ***************************************************/ /* * Question: * Assume that you are given a 10-element array of characters. The values * in the array are limited to '+','-', and '0' throught '9'. Each * character of the array represents a single digit of a number along with * its sign. * Write a program that converts the characters in the array into the * correct integer value and display its square. * * Ex: if Arr="-361" the it's corresponding square value is 130321. * * By doing this, you have almost implemented the atoi function!! */ #include<stdio.h> #define MAX_LENGTH 15 main() { char arr[MAX_LENGTH]; int value; printf("Give the array: "); scanf("%s",arr); value=my_atoi(arr); printf("%d\n",value*value); } int my_atoi(char *str) { int i=0,len,value=0; i=str[0]=='-'||str[0]=='+'?1:0; len=strlen(str); for(;i<len;i++) if(str[i]<'0' || str[i]>'9') { printf("%s is not an integer\n",str); return 0; } else value=value*10+str[i]-'0'; return str[0]=='-'?value*-1:value; }