NVIDIA Agilent Technologies Interview Question
Software Engineer / DevelopersKewl solution. I like that int_val = (int_val*10) + (*str - '0')%10; It is very cryptic and really nice.
Here is a similar solution...just a different framework.
#include <iostream.h>
long Atoi(const char*);
int main(void)
{
while(1)
{
char s[20];
cout<<"Enter string to convert to integer (q to quit): ";
cin>>s;
if(!strcmp(s,"q")) break;
cout<<"Standard atoi function's output: "<<atoi(s)<<endl;
cout<<"Custom made Atoi function's output: "<<Atoi(s)<<endl;
}
}
long Atoi(const char *str)
{
short negative = 1;
long intVal=0;
if(str[0] == '-')
{
negative = -1;
str++;
}
else if(str[0] == '+')
str++;
while(*str >= '0' && *str++ <= '9')
intVal = intVal*10 + (*(str-1)-'0');
return negative*intVal;
}
/* with minimal error checking
unsigned int atoi(char* number) {
if (number == NULL || strcmp(number,"") == 0) {
return 0;
}
unsigned int iNumber = 0;
while (number != NULL && strcmp(number,"") != 0) {
if(*number <= '9' && *number >= '0' ) {
iNumber = (iNumber << 3) + (iNumber << 1)+(*number++ - '0');
}
else {
return 0;
}
}
return iNumber;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
unsigned int atoi(char* number);
int main() {
int number;
number = atoi("35.223");
printf("Number is %d\n",number);
return 0;
}
int atoi(char* number) {
int iNumber = 0; //initialize it to 0
int loopIndex;//index that loops through the number string
int strLen = 0;
char *decimalPoint;
if(number != NULL) { //string points to a null character
if(strcmp(number, "") != 0) {//check if the string is not empty
strLen = strlen(number);
if(strchr(number, '.') != NULL) {
decimalPoint = strrchr(number, '.'); //Truncate the digits after the decimal point
strLen = decimalPoint-number;
}
if(number != NULL) {
for(loopIndex = strLen-1; loopIndex >= 0; loopIndex--) {
if(number[loopIndex] <= '9' || number[loopIndex] >= '0') {
iNumber+=(pow(10, ((strLen-1)-loopIndex)))*(number[loopIndex]-'0');
}
}
}
}
}
return iNumber;
}
int sum=0;
int mult=1;
int start= first(s,'.'); // find first occurance of .
if (start==-1) start=strlen(s)-1; // no ., start at end
for ( i=start; i>=0 ; i--) {
int v= (s[i]-'0') * mult;
sum = sum + v;
}
// yeah, I assume ascii - it wont work with ebcdic or other character sets with holes
// yea, I assume no commas.
// yeah, I didn't write first()... most langs have such a fuction to find the first occurance of a character in a string
int sum=0;
int mult=1;
int start= first(s,'.'); // find first occurance of .
start--;
if (start < 0 ) start=strlen(s)-1; // no ., start at end
for ( i=start; i>=0 ; i--) {
int v= (s[i]-'0') * mult;
sum = sum + v;
}
// yeah, I assume ascii - it wont work with ebcdic or other character sets with holes
// yea, I assume no commas.
// yeah, I didn't write first()... most langs have such a fuction to find the first occurance of a character in a string
#include<stdio.h>
#include<limits.h>
typedef enum {
WHITE_SPACE,
GETDIGITS,
FINISHED
} get_state;
typedef enum {
POSITIVE,
NEGATIVE
} get_sign;
int get_int(char * ch){
get_state state = WHITE_SPACE;
get_sign sign = POSITIVE;
int value = 0;
int limit = INT_MAX / 10;
int dig;
if(*ch){
while(state != FINISHED){
dig = (unsigned int)*ch++;
switch(state){
case WHITE_SPACE:
if(dig == ' '){
;
}
else if(dig == '+'){
state = GETDIGITS;
}
else if(dig == '-'){
state = GETDIGITS;
sign = NEGATIVE;
}
else if(dig >= '0' && dig <= '9'){
value = dig - '0';
state = GETDIGITS;
}
else{
state=FINISHED;
}
break;
case GETDIGITS:
dig = dig - '0';
if( !( ( dig >= 0 ) && ( dig <= 9 ) ) ){
state=FINISHED;
break;
}
if( value < limit ||
( (value ==limit) &&
(((sign == POSITIVE) && (INT_MAX - value * 10 >= dig))
|| ((sign == NEGATIVE) && (- INT_MIN - value * 10 >= dig)))) ){
value = value * 10 + dig ;
}
else{
if(sign == NEGATIVE)
value=INT_MIN;
else
value=INT_MAX;
state=FINISHED;
}
break;
case FINISHED:
default:
break;
}
}
}
if( sign == NEGATIVE )
value = value * -1;
return value;
}
int main(){
char str[20];
while(1){
printf("\nEnter Number: ");
fgets(str , sizeof(str),stdin);
fflush(stdin);
printf("\nYou Entered = %d ",get_int(str));
}
}
// convert.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int power( int x, int n)
{
if(n == 0)
return 1;
else
return x * power( x , n - 1 );
}
int my_atoi(const char *input)
{
int return_value = 0;
int digit_count = 0;
if ( ! input ) return 0;
char *temp = (char*)input;
/* Check if the input contain any alphabets */
while ( *temp != '\0' )
{
if (((int)(*temp) < 48 ) || ((int)(*temp) > 57 ) )
{
return return_value;
}
digit_count++;
temp++;
}
temp = (char*)input;
digit_count--;
while ( *temp != '\0' )
{
int t = power( 10, digit_count-- );
int m = (char)(*temp) - '0';
return_value = return_value + (t * m);
temp++;
}
return return_value;
}
int _tmain(int argc, _TCHAR* argv[])
{
printf ("-----> %d\n",my_atoi(""));
printf ("-----> %d\n",my_atoi("12"));
printf ("-----> %d\n",my_atoi("12345"));
printf ("-----> %d\n",my_atoi("5555"));
printf ("-----> %d\n",my_atoi("12A45"));
return 0;
}
Here is my code...
#include<stdio.h>
void main()
{
char str[100];
char * ptr;
printf("Enter the string \n");
ptr = gets(str);
int i =0;
int val = 0;
while(str[i]!='\0' && (str[i] >='0' && str[i] <='9' || str[i] == '-'))
{
if(str[i] == '-')
{
i++;
continue;
}
val = str[i]-'0' + val*10;
i++;
}
if(str[0] == '-')
printf("The value is %d \n",0- val);
else
printf("The value is %d \n", val);
}
/* Return value:
- PR March 31, 2006integer value of the string passed
Functionality:
Below function works for examples as shown below:
"0" -> output = 0
"1" -> output = 1
"123" -> output = 123
"-1" -> output = -1
"+1" -> output = 1
" 0" -> output = 0
" -1" -> output = -1
" +1" -> output = 1
"00" -> output = 0
"-01" -> output = -1
"+01" -> output = 1
"00123" -> output = 123
"12300" -> output = 12300
" -0123" -> output = -123
" +0123" -> output = 123
"*123" -> output = 123
"123*" -> output = 123
"*123*" -> output = 123
"*-123" -> output = -123
“*+123” -> output = 123
“123-“ -> output = 123
“123+” -> output = 123
"*12*3" -> illegal input -> undefined output = 12
"*-*12" -> illegal input -> undefined output = 0
Algorithm:
1. Skip any non digit characters, which are not '-'
2. If you hit a '-', it means its a negative number. Skip '-' and make
is_negative boolean true.
3. From this point, while you have continuous digits, do
a. Every iteration of the digit, multiply the int_val by 10 and add (digit value%10)
4. If is_negative is true, multiply the int_val by -1 and return,
else return int_val */
int atoi(char *str)
{
int is_negative = 0;
int int_val = 0;
/* Skip non digit characters excluding '-' */
while (!isdigit(*str) && (*str != '-'))
{
str++;
}
/* Number is negative */
if (*str == '-')
{
is_negative = 1;
str++;
}
/* Compute the int_val for contiguous digits */
while (isdigit(*str))
{
int_val = (int_val*10) + (*str - '0')%10;
str++;
}
return ((is_negative) ? (int_val * -1): int_val);
}