## Saturday, July 7, 2007

### Write your own C program to implement the atoi() function

The prototype of the atoi() function is ...

int atoi(const char *string);

Here is a C program which explains a different way of coding the atoi() function in the C language.

#include

int myatoi(const char *string);

int main(int argc, char* argv[])
{
printf("\n%d\n", myatoi("1998"));
getch();
return(0);
}

int myatoi(const char *string)
{
int i;
i=0;
while(*string)
{
i=(i<<3) + (i<<1) + (*string - '0');
string++;

// Dont increment i!

}
return(i);
}

Try working it out with a small string like "1998", you will find out it does work!.

berit said...

Goddam!

zubair said...

Phwwww .. nicely done ..

Anonymous said...

Could you please explain what you are doing in your code. I am not a C programmer and its really difficult for me to follow your code. It would be great if you post some explanation for your code.

Thanks
Paresh

AzulSky said...

Good approach but u if the string is"1998a", the result is wrong. You need to keep a check for these character.

Anonymous said...

very clever idea...

for those who didn't get it:

i<<3 is equivalent of multiplying by 2*2*2 or 8

so i<<3 + i<<1 means multiply by 10

Anonymous said...

why dint u jus use i=i*10 +(*string-'o');
why did u use left shiftin operation?

Anonymous said...

Computers can quickly use bit-wise shifts to multiply (shift left) and divide (shift right) by powers of two. A simple extension can be used to multiply by 10 by multiplying by five (with one two-bit-shift and one addition), and then another one-bit-left shift.

Anonymous said...

wot is basically done here?
why do u multiply 'o' by 10 and subtract by 'o'?
why did u choose 'o'?

Anonymous said...

nicely done w/ the shift.

Left out few special cases though:
1) did not consider decimal place.
2) did not consider sign
3) how about checking if the string is too big to be stored in an integer?

Anonymous said...

// atoi
// precondition: a string ("1234")
// postcondition: a number converted from the input string
// special case: if a string is too long, the converted number could be bigger than 2^32-1 ?
int myatoi (char* string)
{
int sign = 1;
// how many characters in the string
int length = strlen(string);

// handle sign
if (string[0] == '-')
{
sign = -1;
i = 1;
}

for (i; i < length; i++)
{
// handle the decimal place if there is one
if (string[i] == '.')
break;
number = number * 10 + (string[i]- 48);
}

number *= sign;

return number;
}

Chhewang said...

GOOT IT!

phongvu1991 said...

#include "stdafx.h"
#include

#define MAX 100

using namespace std;

int my_atoi(char *s);

int main()
{
char s[MAX];

cout << "\n Nhap vao mot chuoi : " ;
cin.getline(s, MAX, '\n');

cout << " Sau khi atoi : " << my_atoi(s) << endl;

return 0;
}

int my_atoi(char *s)
{
char *copystr = strdup(s);

int dem = 0;

for(int i = 0; i < strlen(copystr); i++)
{
if(copystr[i] >= '0' && copystr[i] <= '9')
{
dem =(dem<<3) + (dem<<1) + (copystr[i] - '0');
}
}
return dem;
}

Anonymous said...

sahil said...

what if number are inbetween a string?

#include
#inlude

int main(){

char string[] = "RAS23A42";
int sign = 1, number=0;

char cleanStr[strlen(string)];
memset(cleanStr,0,sizeof(cleanStr));

int i,j;
for(i=0,j=0;i<strlen(string);i++){

//if negative number
if(string[i]=='-')
sign = -1;
if(isdigit(string[i])
cleanStr[j++] = string[i];

}// end 'for' loop

//cleanStr now contain string :2342

i=strlen(cleanStr);
while(i<strlen(cleanStr)){
number = number * 10 + (cleanStr[i] - 48);
++i;
}
//change sign
k * = sign;

//print result
printf("%d",k);

return 0;
}

Manish said...

This code above is not working.

Manish said...

I meant sahil's code. I tried to make it work, but it gives me wired results. If possible please provide the code which takes care of sign and any characters in given string.

Anonymous said...

What if the input is a overflow case;

Anonymous said...

nyc post

Anonymous said...

can anyone post the solution?

Anonymous said...

good

nina said...

wel i hav aprogram which i saw in one of the books which has its function implementation as such
int atoi(char s[])
{
int i,n;
n=0;
for(i=0;s[i]>='0'&&s[i]<='9';i++)
n=10*n+(s[i]-'0');
return n;
}
i am getn the output as 0,what do i,is there any mistake in the code??

Umopepisdn said...

It's a neat trick, but I wouldn't drop this in an interview without an accompanying spiel about why using arcane syntax in real code that somebody else might have to maintain someday is never okay. :P

Mohamed Hassan said...
This comment has been removed by the author.
Anonymous said...

I pay a visit everyday a few sites and sites to read
posts, except this blog presents feature based writing.

ciekawy blog tutaj, http://wiki.
terrot.org/index.php?title=Utilisateur:DanaMarin

Anonymous said...

Hi there! I realize this is kind of off-topic but I needed to ask.
Does operating a well-established blog such as yours require a massive
amount work? I'm completely new to writing a blog however I do write in my journal every day. I'd like
to start a blog so I can share my experience and views
online. Please let me know if you have any suggestions
or tips for brand new aspiring blog owners. Thankyou!
zobacz ciekawÄ… stronÄ™ na ten temat, http://www.

dir.kszynka.ayz.pl/
Also see my site - zobacz ciekawÄ… stronÄ™ na ten temat

Anonymous said...

If some one needs expert view concerning running a blog after that i recommend him/her to pay a visit this webpage, Keep up
the nice work. ciekawy blog, http://www.
apparelcycle.com/index.php?do=/profile-70877/info/
My web page: ciekawy blog

Anonymous said...

Wow! I'm genuinely enjoying the layout of your blog. Are you using a custom made template or is this freely available to all individuals? If you don't
want to say the name of it out in the general public, please be sure
to contact me at: kieran-flora@bigstring.com. I'd love to get my hands on this template! Many thanks.

Take a look at my web blog - furnace repair affordable

Anonymous said...

I'm truly enjoying the design and layout of your website. It's a very easy on
the eyes which makes it much more pleasant for me to come here and visit more often.
Did you hire out a developer to create your theme? Exceptional work!

My homepage: lennox furnace troubleshooting

Anonymous said...

Hey! I know this is somewhat off topic but I was wondering which blog
platform are you using for this website? I'm getting tired of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform. I would be fantastic if you could point me in the direction of a good platform.

Visit my homepage rheem furnace troubleshooting

Anonymous said...

I've loaded your site in Three completely different web browsers and I must say this website loads a lot faster then most. Would you mind e-mailing me the name of your web hosting company? My personal e-mail is: carlhomanning@gmail.com. I will even sign up through your own affiliate link if you'd like.
Thanks a lot

my web blog ... short homes extended stay hotels

Anonymous said...

Appreciating the hard work you put into your site and in depth information you present.

It's awesome to come across a blog every once in a while that isn't the same old rehashed information.

my web page: furnace troubleshooting flowchart

Anonymous said...

Iím not that much of a online reader to be honest but your sites really nice,
keep it up! I'll go ahead and bookmark your site to come back down the road. Many thanks

Anonymous said...

Hi superb blog! Does running a blog such as this
require a great deal of work? I've virtually no expertise in computer programming but I was hoping to start my own blog soon. Anyway, should you have any recommendations or tips for new blog owners please share. I know this is off topic nevertheless I simply wanted to ask. Appreciate it!

Anonymous said...

A design like yours with a few simple tweeks would really
make my blog jump out. Please let me know where you got your design.
Thanks

Here is my weblog ... home improvement company

Anonymous said...

Hey there! Do you use Twitter? I'd like to follow you if that would be ok. I'm absolutely enjoying your blog and look forward to new updates.

Here is my homepage: gymnastics clothing

Anonymous said...

Exploring digg.com I noticed your site bookmarked as: Blogger: Interview Questions.
I am assuming you book marked it yourself and wanted to ask if
social bookmarking gets you a bunch of traffic? I've been thinking about doing some social bookmarking for a few of my websites but wasn't sure if it would
produce any positive results. Many thanks.

Feel free to surf to my homepage :: personalized gym bags embroidered

Anonymous said...

First of all I would like to say fantastic blog! I had a quick question which I'd like to ask if you do not mind. I was curious to know how you center yourself and clear your thoughts prior to writing. I have had a hard time clearing my mind in getting my ideas out there. I do enjoy writing but it just seems like the first 10 to 15 minutes are usually wasted just trying to figure out how to begin. Any suggestions or tips? Appreciate it!

Also visit my weblog; exercise ball ab workouts

Anonymous said...

Hi! I'm at work surfing around your blog from my new iphone 4! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the superb work!

Visit my web-site physical therapy exercises for lower back pain

Anonymous said...

Good day! I could have sworn I've been to this site before but after checking through some of the post I realized it's new to me.
Anyways, I'm definitely glad I found it and I'll be
bookmarking and checking back often!

Here is my blog post - 24 hour fitness gym membership

Anonymous said...

It is appropriate time to make some plans for the
future and it is time to be happy. I have read this post
and if I could I desire to recommend you some fascinating issues or suggestions.
I wish to read even more issues approximately it!

my web site - hairdresser Parktown North

Anonymous said...

Great web site you have here.. It's hard to find good quality writing like yours these days. I seriously appreciate people like you! Take care!!

Look at my weblog; wooden jungle gyms gauteng

Anonymous said...

At this time it seems like Expression Engine is the best blogging
platform available right now. (from what I've read) Is that what you're using on your blog?

Check out my web site blurty.com

Anonymous said...

Hi! I'm at work browsing your blog from my new iphone 4! Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the excellent work!

Check out my blog: maternity workout clothes sale

Anonymous said...

Very quickly this website will be famous amid all blogging and site-building people, due to it's good articles or reviews

Visit my web-site; shuttle service Gauteng

Anonymous said...

I was able to find good information from your content.

Also visit my blog ... skin tightening Cape Town

Anonymous said...

You have made some really good points there. I checked on the
net to find out more about the issue and found most individuals
will go along with your views on this web site.

Anonymous said...

I would like to thank you for the efforts you've put in penning this site. I'm hoping to view the same high-grade blog posts by you in the
future as well. In fact, your creative writing abilities has encouraged me
to get my own website now ;)

Anonymous said...

You are so cool! I do not believe I have read anything like
that before. So good to discover someone with some unique thoughts on this subject matter.
Seriously.. many thanks for starting this up.

This website is something that is required on the internet, someone with
some originality!

Anonymous said...

Hello, i think that i saw you visited my weblog so i came to “return the favor”.
I am attempting to find things to enhance my web site!
I suppose its ok to use some of your ideas!!

Anonymous said...

I read this piece of writing completely concerning the resemblance of most
up-to-date and earlier technologies, it's remarkable article.

Anonymous said...

Hey There. I discovered your blog the use of msn. That is a really well written article.
I will be sure to bookmark it and come back to
I will definitely return.

Anonymous said...

Hey There. I discovered your blog the use of msn.
That is a really well written article. I will be sure to bookmark it and come back to read extra of your helpful information.
Thank you for the post. I will definitely return.

Anonymous said...

Hey superb website! Does running a blog similar to this require a massive amount work?
I've virtually no understanding of computer programming however I had been hoping to start my own blog in the near future. Anyway, if you have any ideas or techniques for new blog owners please share. I know this is off topic however I just had to ask. Kudos!

Take a look at my page - blogster.com

Anonymous said...

It's wonderful that you are getting ideas from this piece of writing as well as from our argument made at this place.

Also visit my webpage website

Anonymous said...

Hey there! This is my first comment here so I just wanted to give a quick shout out and
tell you I truly enjoy reading through your
articles. Can you recommend any other blogs/websites/forums that go
over the same topics? Thanks for your time!

Feel free to visit my website :: 9:31 pm

Anonymous said...

I wanted to thank you for this wonderful read!
! I absolutely enjoyed every bit of it. I have got you
book-marked to check out new things you post…

my homepage: beam central vacuum systems Gauteng

Anonymous said...

First of all I would like to say awesome blog!
I had a quick question in which I'd like to ask if you do not mind. I was curious to find out how you center yourself and clear your mind before writing. I've had
trouble clearing my mind in getting my thoughts out.
I do take pleasure in writing but it just
seems like the first 10 to 15 minutes are generally lost just
trying to figure out how to begin. Any recommendations or hints?
Thanks!

Have a look at my blog post ... waste management Cape Town

Anonymous said...

Wow! I'm truly enjoying the style and design of your blog. Are you using a customized theme or is this freely available to all individuals? If you don't want to
say the name of it out in the general public, please e-mail me at: antje.
nesmith@web.de. I'd love to get my hands on this template! Thanks.

My website ... home improvment

Anonymous said...

Hello! This is kind of off topic but I need some help from an established blog.
Is it difficult to set up your own blog? I'm not very techincal but I can figure things out pretty quick. I'm thinking about setting up my own but I'm not sure where to start. Do you have any ideas or suggestions? Many thanks

Have a look at my weblog electronics for kids age 6

Anonymous said...

Hey, I think your blog might be having browser compatibility issues.
When I look at your blog in Ie, it looks fine but when opening in Internet Explorer,
it has some overlapping. I just wanted to give you a quick heads up!
Other then that, amazing blog!

My web page yahoo digital camera buying guide

Anonymous said...

Hi there fantastic blog! Does running a blog such as
this require a great deal of work? I have very little expertise in coding however I had
Anyhow, should you have any suggestions or techniques for new blog owners please share.
I know this is off subject nevertheless I simply had to ask.
Thank you!

Anonymous said...

Hi! My partner and I often write guest articles or blog posts for other weblog
owners to help gain publicity to our work, as well as provide wonderful articles to
website owners. It really is a win win situation!
If you're interested feel free to email me at: reyesgarris@gmail.com so we can discuss further. Kudos!

my web site - buy ethernet cables bulk

Anonymous said...

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I
get several emails with the same comment. Is there any way you can remove people from that
service? Many thanks!

my homepage; plumbing services tucson az

Anonymous said...

Howdy! I know this is kinda off topic but I'd figured I'd ask.
writing a blog article or vice-versa? My site discusses a lot of
the same topics as yours and I feel we could greatly benefit from each other.
If you happen to be interested feel free to send me an email.

I look forward to hearing from you! Wonderful blog by the
way!

Feel free to surf to my blog where to buy ballet shoes for kids

Anonymous said...

Howdy! Do you know if they make any plugins to assist with Search Engine Optimization?
I'm trying to get my blog to rank for some targeted keywords but I'm
not seeing very good gains. If you know of any please share.
Thank you!

My web-site ... silicon chip dual tracking power supply

Anonymous said...

Hello would you mind stating which blog platform you're using? I'm looking to start my own blog in the near future but I'm having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I'm looking for something completely unique.

Feel free to visit my page :: flash memory card reader firewire

Anonymous said...

Hi there! This post could not be written any better! Reading through this post reminds me of my
good old room mate! He always kept talking about this. I will forward this write-up to him.
Fairly certain he will have a good read. Many thanks for sharing!

Also visit my web site - cool high tech watches for men

Anonymous said...

This design is steller! You definitely know how to keep a reader entertained.

Between your wit and your videos, I was almost moved to start my own blog (well, almost.
..HaHa!) Fantastic job. I really enjoyed what you had to say,
and more than that, how you presented it. Too cool!

My weblog ... wireless sd memory card reader

Anonymous said...

Howdy I am so excited I found your site, I really found you by accident, while I was researching on Askjeeve for something
else, Anyhow I am here now and would just like to say thanks for
a tremendous post and a all round exciting blog (I also love the theme/design),
I donít have time to read it all at the moment but I have
will be back to read a great deal more, Please do keep up the awesome work.

Here is my web page appliance repair school online

Anonymous said...

Have you ever thought about creating an e-book or guest authoring on other
blogs? I have a blog centered on the same subjects
you discuss and would really like to have you share some
stories/information. I know my visitors would
value your work. If you're even remotely interested, feel free to shoot me an e-mail.

Here is my web site diets that work fast

Anonymous said...

If some one wants eхpert ѵiew regarԁing runnіng a
blog afterωard i pгoρose him/her tο рay a νisit this web sitе, Κeeρ up the faѕtіdious work.
garcinia cambogia - garcinia cambogia
- http://quickies.heinsberger-info.de http://www.vanmorgan.tv/groups/why-folks-like-to-consume-garcinia-cambogia
- garcinia cambogia - garcinia cambogia

Fееl free to surf to my blog post; www.f1mnenie.ru

Anonymous said...

I would like to thank you for the efforts you have put in writing this site.

I'm hoping to see the same high-grade content by you later on as well. In fact, your creative writing abilities has motivated me to get my very own blog now ;)

Feel free to visit my homepage - meningitis symptoms in adults

Anonymous said...

I would like to thank you for the efforts you have put in writing
this site. I'm hoping to see the same high-grade content by you later on as well. In fact, your creative writing abilities has motivated me to get my very own blog now ;)

my web-site ... meningitis symptoms in adults

Anonymous said...

even as looking for a related matter, your web site came up, it appears great.
I have bookmarked it in my google bookmarks.
Hello there, simply turned into alert to your weblog thru Google, and found that it's truly informative. I'm going to watch out for brussels.
I will appreciate in the event you proceed this in future. Lots
of other folks shall be benefited out of your writing.
Cheers!

my homepage; kliknij

Anonymous said...

Hello there! I could have sworn I've been to this site before but after browsing through many of the posts I realized it's new to me.
Regardless, I'm definitely delighted I discovered it and I'll be book-marking it and checking back regularly!

Here is my web site - kliknij

Anonymous said...

I want to to thank you for this wonderful read!! I certainly enjoyed every bit of it.
I've got you saved as a favorite to check out new stuff you post…

Here is my web page kliknij

Anonymous said...

This info is priceless. When can I find out more?

my page; kliknij

Anonymous said...

Hi there! Do you use Twitter? I'd like to follow you if that would be ok. I'm definitely enjoying
your blog and look forward to new posts.

Also visit my site - kliknij

Anonymous said...

Hi there this is kind of of off topic but I was wondering
if blogs use WYSIWYG editors or if you have to manually code with HTML.

I'm starting a blog soon but have no coding skills so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

my page: kliknij

Anonymous said...

I am really grateful to the holder of this website who has shared this enormous piece
of writing at at this time.

Here is my weblog ... kliknij

Anonymous said...

I delight in, lead to I discovered just what I was looking
for. You have ended my 4 day lengthy hunt! God Bless
you man. Have a nice day. Bye

Look at my blog :: kliknij

Anonymous said...

If you are going for best contents like myself, simply go to see this web site daily since it provides quality contents, thanks

Here is my web-site :: kliknij

Anonymous said...

Nice post. I learn something totally new and challenging on sites I stumbleupon everyday.
It will always be exciting to read content from other writers
and practice something from their sites.

My page :: kliknij

Anonymous said...

Hello, after reading this amazing piece of writing
i am too cheerful to share my knowledge here with friends.

My web blog: kliknij

Anonymous said...

Hi there, I check your new stuff regularly. Your humoristic style is awesome, keep up the
good work!

my blog post :: kliknij

Anonymous said...

We stumbled over here from a different website
and thought I may as well check things out. I like what I
see so i am just following you. Look forward to going over your web page repeatedly.

my weblog - kliknij

Anonymous said...

Hey there! I know this is kind of off topic but I was wondering if you knew where I could find
a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having problems
finding one? Thanks a lot!

Check out my web site - kliknij

Anonymous said...

Hello my loved one! I want to say that this post is amazing,
great written and include approximately all important infos.

Also visit my site ... kliknij

Anonymous said...

I love looking through an article that can make men and women think.
Also, thanks for allowing me to comment!

my site; kliknij

Anonymous said...

Excellent blog! Do you have any suggestions for aspiring writers?
I'm planning to start my own website soon but I'm a little lost on everything.
Would you propose starting with a free platform like Wordpress or
go for a paid option? There are so many choices out there that I'm totally overwhelmed .. Any suggestions? Appreciate it!

Feel free to surf to my weblog kliknij

Anonymous said...

Wow, incredible blog structure! How lengthy have you ever
been running a blog for? you made running a blog look easy.
The overall look of your site is great, let
alone the content material!

my homepage :: importantnews.ca/CyrusBla

Anonymous said...

Wе stumbled over here cоmіng from
a ԁifferеnt website аnd thοught I
may аs wеll chесk thingѕ out.
I like what I see so now i'm following you. Look forward to looking over your web page yet again. rippln - rippln app - rippln mobile

Anonymous said...

Thank you for the auspicious writeup. It in fact was a amusement account it.