# Thread: Converting String to integers

1. ## Converting String to integers

Hello all,

I can not find my K & R book and I am trying to find examples that convert strings to integers and floating point numbers without using atoi() and atof() function. (I have those)

I am using this in one of the course I am teaching this summer. I was pretty sure there was an example in the K & R book?

I have trying some simple searches at google and at this site- with little luck.

Some direction would help

Thanks

Mr. C.

2. You mean like say
sscanf
strtol
strtod
?

3. Brian,

I look they still all appear to use ato()- which is not what I want.

4. Salem,

No, I was looking for something like:

http://www.fredosaurus.com/notes-cpp...tring2int.html

Just looking for more comprehensive examples..

5. One of the examples in the FAQ uses strtol instead of atoi.

6. Looks like you found something like http://www.fredosaurus.com/notes-cpp...tring2int.html
<shrug>

7. I noticed this

The CS106 Library: strutils.hIn addition to the standard library support for strings, there are a few extensions that the CS106libraries provide. To use these functions, the strutils.h function must be included:#include <strutils.h>Note for CS106B StudentsThese functions are identical to the conversion functions from CS106A except that theypass and return C++ strings instead of char*.IntegerToString, RealToString, StringToInteger, StringToReal: Often yourprograms will need to convert a string to a number or vice versa. These functions do just that,with the integer functions operating onintand the real functions ondouble.example program:
Code:
```#include <string>
#include <iostream>
#include "strutils.h"
#include "genlib.h"
int main() {
string str1 = "5.6";
double num = StringToReal(str1);
string str2 = IntegerToString(45);
cout << "The original string is " << str1 << "." << endl;
cout << "The number is " << num << "." << endl;
cout << "The new string is " << str2 << "." << endl;
eturn 0;}```
output:The original string is 5.6.The number is 5.6The new string is 45.Any integer or real can be safely converted to a string. However, converting in the otherdirection, if you pass an improperly formatted string to convert to an integer or real, an error israised by the conversion functions.ConvertToLowerCase, ConvertToUpperCase: These functions take a string, and return a newstring with all letters in lower or upper case respectively. These can be used to change twostrings to a uniform case before comparing to allow a case-insensitive comparison.
--------------------------------------------------------------------------------
Page 12
-12-
--------------------------------------------------------------------------------
Page 13
-13-example program:#include <string>#include <iostream>#include "strutils.h"#include "genlib.h"int main() {string appleFruit = "apples";string orangeFruit = "ORANGES";cout << "Do " << appleFruit << " come before "<< orangeFruit << "? ";if (appleFruit < orangeFruit)cout << "Yes!" << endl;elsecout << "Nope...." << endl;string lowerOrangeFruit = ConvertToLowerCase(orangeFruit);cout << "Do " << appleFruit << " come before "<< lowerOrangeFruit << "? ";if (appleFruit < lowerOrangeFruit)cout << "Yes!" << endl;elsecout << "Nope...." << endl;return 0;}outputo apples come before ORANGES? Nope....Do apples come before oranges? Yes!"There are only 10 kinds of people in the world. Those who know binary and those who don't" -- Anonymous

So I was just wondering how they coded that user defined library

8. Mister C
CS Author and Instructor
You've got to be kidding me. You can't figure out how to turn a string into a number without using any functions not written by you? You're teaching?

Right. That explains all of the 'void main' people we get here.

Quzah.

9. Quzah

Yes, my C# book was published in January. Grading APCS papers in two weeks as well.

I know that it is int main- although many of my associates still use void main.

I guess I do not know how to program :

10. What was the name of that C# book?

11. If you can't turn a string into an integer or floating point numbers without using any non-user created functions, then no, you don't know how to program. Or maybe you just lack the logic necessary to figure it out. However, since there is a very common related homework assignment that gets posted here all the time, I should be surprised you don't know how to do it. What with you grading all of those papers, you're bound to run across the answer some time.

[edit=To further a point.]
Besides, what does reading an answer key have to do with knowing how to program?
[/edit]

Quzah.

12. I can not find my K & R book and I am trying to find examples that convert strings to integers and floating point numbers without using atoi() and atof() function. (I have those)
After reading through this thread, it sounds like what you were really asking is: How would I write my own functions to convert strings to integers and floating point numbers?

13. cout is C++ and this is the C forum.

Try this:

Code:
```#include <ctype.h>

int dwks_atoi(char *string) {
int i = 0, x;

for(x = 0; string[x]; x ++) {
if(isdigit(string[x])) {
i *= 10, i += (string[x]-'0');  /* not portable */
}
}

return i;
}```
I know i += (string[x]-'0') isn't portable, you could replace it with the following:

Code:
```switch(string[x]) {
case '0': i += 0; break;
case '1': i += 1; break;
/* ... */
}```
dwk

14. actually if you knew that the entire string was just numeric then it is portable. Its required that the character set have the characters '0' through '9' in sequential and contiguous order.

But the point of fact of this thread is that Mister "I wish I knew C" should know how to do this since he teaches C programming and this is an insanly common problem (So far I've done this in every programming class I've taken)