“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
Read more comments...
Write a function that takes in a string parameter and checks to see whether or not it is an integer, and if it is then return the integer value.
2,902 Views | (3 votes, avg: 2.33)
int str2int(const char *str){ int value = 0; int sign = (str[0] == ‘-’)?-1:1; int i = (str[0] == ‘-’)?1:0; char ch;
while(ch = str[i++]) { if ((ch >= ‘0′)&&(ch <= ‘9′)) value = value*10 + (ch - ‘0′); else return 0; } return value*sign;}
#include <stdio.h>int strtoint(char *s){int index = 0, flag = 0;while( *(s+index) != ‘
int str2int(const char *str)
{
int value = 0;
int sign = (str[0] == ‘-’)?-1:1;
int i = (str[0] == ‘-’)?1:0;
char ch;
while(ch = str[i++])
{
if ((ch >= ‘0′)&&(ch <= ‘9′))
value = value*10 + (ch - ‘0′);
else
return 0;
}
return value*sign;
}
#include <stdio.h>
int strtoint(char *s)
{
int index = 0, flag = 0;
while( *(s+index) != ‘