Hi all,
I have to write a program that will input a string and convert the string to an integer value. The program should then output the
integer value multiplied by 2. Also im not supposed to use any built in functions.Can anyone help me?
Thanks a million!
Sana Zafar

Recommended Answers

All 2 Replies

If you look at any standard ascii chart you will see that every character on the keyboard has a decimal value. The digits '0' through '9' have a decimal value of 48 to 57. To convert any digit to an integer all you have to do is subtract '0' (or 48) from it. So, the digit '1' when coverted will be '1' - '0', which is 49 - 48 = 1. Now to solve your problem just iterate through each character in the string and subtract '0', then add the result to the sum variable. For example sum = (sum * 10) + '1' - '0';

Thanks a bunch for your help!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.