| | |
question about string element
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
I got a question about c++ strings.
Let's say string text = "0R14"
and I access each element using text[0],text[1] etc
does text[0] return an integer 0? or a character '0'?
if it doesn't return an integer, how do I convert individual string element to integer type if I need to do integer calculations?
Let's say string text = "0R14"
and I access each element using text[0],text[1] etc
does text[0] return an integer 0? or a character '0'?
if it doesn't return an integer, how do I convert individual string element to integer type if I need to do integer calculations?
3
#4 Oct 8th, 2009
atoi(text[0]) will not work because atoi expects a char*, not a char. That half of NeoKyrgyz's answer was basically useless and dumb.If you want to convert the character "c" representing the decimal digit n to the integer n, you can do so with
(c - '0') . That's the same as what NeoKyrgyz said, because '0' is just a fancy way of writing 48 . You'll note that '0' == 48, '1' == 49, ..., '9' == 57. If text is a string containing "0R14", then text[0] will evaluate to 48.atoi is for converting c-style strings containing sequences of digits into numbers, e.g. "123" -> 123
All my posts may be redistributed under the GNU Free Documentation License.
![]() |
Similar Threads
- Prototyping an element (JavaScript / DHTML / AJAX)
- data string to array (C#)
- Question about string pattern matching (Java)
- Quick Method Question (Java)
- Interpret a string as name of an array element name (Visual Basic 4 / 5 / 6)
- Question about string manipulation (C)
- String Object Immutable Confusion (Java)
- changing from onclick to onsubmit? possible? (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: animate a triangle in c
- Next Thread: Multiple "cout"s and "if"s within an If statement
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






