Thread: How to return a character in string and convert a char to int ?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    30

    How to return a character in string and convert a char to int ?

    I have a string and I'd like to check whether a character at a certain position is a digit or a letter.

    I would do it like this:

    char *mynumber - comes from function parameter

    int a = atoi(mynumber[0]);
    int b = atoi(mynumber[1]);
    int c = atoi(mynumber[2]);

    if (isdigit(mynumber[0]) && (b - a = 1) ...)

    But it keeps telling me "passing arg 1 of `atoi' makes pointer from integer without a cast", so I guess it doesn't like mynumber[i] which a thought would work for returning a character from a string.

    Any suggestions ?

    Best wishes, D5

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're passing a char, but atoi expects char*.
    You must pass a string, not a char.
    To check if a certain character is a digit, you can use the function isdigit().
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    30
    I am using the isdigit(), but I want to use it only on a specifit character in string not the whole string. Same thing with atoi(). So what's the workaround ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The answer is right in front of you. Isdigit can be used to validate a single char within a string.
    Atoi doesn't work that way. Atoi converts a string to an int. Nothing more. Nothing less. And it does not work on single characters.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I want to use it only on a specifit character in string not the whole string
    You are already doing that correctly with isdigit(mynumber[0]). The error lies with your use of atoi() on characters instead of strings.

    Same thing with atoi().
    Use mynumber[0] - '0' instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    30
    Quote Originally Posted by laserlight View Post
    You are already doing that correctly with isdigit(mynumber[0]). The error lies with your use of atoi() on characters instead of strings.


    Use mynumber[0] - '0' instead.
    What's the point of using int a = atoi("0") ? I dont't want to convert "0" to 0 but the first (or second or fourth or whatever) character in the string to int.

    If I wanted to loop throught all characters in a string and convert them to int one at a time, how would i do that ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's the point of using int a = atoi("0") ?
    I did not suggest that you do that. I suggested that you write: int a = munumber[0] - '0'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by desmond5 View Post
    What's the point of using int a = atoi("0") ? I dont't want to convert "0" to 0 but the first (or second or fourth or whatever) character in the string to int.
    Laserlight already told you. Take the char you want to convert and subtract '0' from it.

    If I wanted to loop throught all characters in a string and convert them to int one at a time, how would i do that ?
    That's exactly (or to a degree, at least) what atoi does - converts an entire string to its integer representation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    30
    Oh, thanks

    Another question, how can I check if all characters in string are the same (ie "aaaaaa") ?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Another question, how can I check if all characters in string are the same (ie "aaaaaa") ?
    Check if every subsequent character is equal to the first character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could do that by setting a pointer to the beginning of the string and walking through it that way. Or you could use an integer index and increment that.

    Code:
    if (isdigit(mynumber[0]) && (b - a = 1) ...)
    I know that's pseudocode, but note that you ought to use == instead of = in if statements, nearly all of the time.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  2. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM