Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 converting string to integer
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Fonzie




PostPosted: Thu Sep 27, 2007 4:59 pm   Post subject: converting string to integer

I need to figure out how to convert a string filled with numbers into an unsigned long long int. The string will have anywhere between 1 and 10 characters. I want it so that the string "9876543210" becomes the integer number 9876543210. for this program I am only allowed to use functions in the string.h library, getchar(), putchar() and printf(). Thanks in advance.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Sep 27, 2007 5:08 pm   Post subject: RE:converting string to integer

Start by figuring out how to get an individual character out of that string (say c = "2") while keeping track of its position from the end (n = 3[rd] char)

Converting a character to an integer is easy, think ASCII values.

That n will let you know where in the string it was.

c = 2
n = 3
c * 10 ** (n-1) = 200

Then just add all of that up.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Fonzie




PostPosted: Fri Sep 28, 2007 2:01 pm   Post subject: Re: converting string to integer

that worked, thanks a lot.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: