Converting String to integers

This is a discussion on Converting String to integers within the C Programming forums, part of the General Programming Boards category; Hello all, I can not find my K & R book and I am trying to find examples that convert ...

  1. #1
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    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.
    Mr. C: Author and Instructor

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    You mean like say
    sscanf
    strtol
    strtod
    ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Brian,

    I look they still all appear to use ato()- which is not what I want.
    Mr. C: Author and Instructor

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Salem,

    No, I was looking for something like:

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

    Just looking for more comprehensive examples..
    Mr. C: Author and Instructor

  6. #6
    FOX
    Join Date
    May 2005
    Posts
    188
    One of the examples in the FAQ uses strtol instead of atoi.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Looks like you found something like http://www.fredosaurus.com/notes-cpp...tring2int.html
    <shrug>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    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
    Last edited by Salem; 05-31-2005 at 12:30 AM. Reason: vague attempt at line unwrapping
    Mr. C: Author and Instructor

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    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 :
    Last edited by Mister C; 05-30-2005 at 06:02 PM.
    Mr. C: Author and Instructor

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What was the name of that C# book?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Last edited by quzah; 05-30-2005 at 07:46 PM.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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?

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting a string of words into correct cases
    By BigFish21 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 12:53 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21