![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Location: Florida, U.S.A.
Posts: 10
Rep Power: 0
![]() |
Convert String to Int
In a recent contest, the problem was to reverse the digits of a number and add that new number to the old number to come up with a newer number. I had the knowledge to reverse the digits of a number by converting the original number to a string and swapping the positions of each character, but to add this new number to the original number, I needed to convert the new number back to an int. I lost the competition because I lacked this knowledge.
How do you convert a String to an Int? -Cool-
__________________
"Give a programmer enough pizza and coke they'll do anything you want." |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3
![]() |
you could of done string.c_str() which would return you the string in an const char* format then you could loop through that new constant array of characters and convert each one to an number use atoi or something else, some else reccommended.
__________________
Quote:
|
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3
![]() |
The easiest way to do it is to use a stringstream
example: cpp Syntax (Toggle Plain Text)
You could also use atoi(), but using stringstreams will be a lot easier for you and a lot more comprehensive. The STL has many things you should read when you encounter a problem. In many occasions, the STL solves the problem. Check this out for a reference.
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Aug 2005
Posts: 68
Rep Power: 4
![]() |
Hello Coll1Net6,
Converting a string to an int is very easy, having in mind that a string is only a representation of a number in the ASCII chart. Having also in mind that a string is nothing more than an array of chars, you can easily manipulate each individual character. So, if your string is composed only of numbers in string format (like '4' or '6'), all you have to do is : int number = string[x] - 48; where x is the index of the char you want converted to int. And the job is done. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Leslie, your method is contingent upon the characters being in an ASCII representation. Such is not always the case. One should use string streams, as recommended, or perhaps strtol or similar. Atoi is not a good choice because errors are indistinguishable from converting the value, zero (and other badnasty results).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3
![]() |
int string_to_int(string number)
{ int num = 0; int temp = 1; for (int i = number.length; i >= 0; i--) { num += atoi(number[i]) * temp; temp *= 10; } return num; } |
|
|
|
|
|
#7 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>for (int i = number.length; i >= 0; i--)
That's an off-by-one error and a syntax error. You want to start at number.length() - 1. >num += atoi(number[i]) * temp; Okay, number is a string, so number[i] is a character. That alone is a type mismatch. You could take the address of that character because atoi expects a C-style string (any number of characters terminated with '\0'), but the string class doesn't guarantee that internal representation. The good news is that your algorithm only expects single characters at a time, so you can use the subtraction trick to turn something like '5' into 5: num += (number[i] - '0') * temp;
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#8 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Are you people reading the responses? ATOI is not a good thang. Here's a quote from cplusplus (emphases mine):
Quote:
I have modified Soulstorm's code to present some alternatives. All are easy. It's certainly preferable to looping through a set of characters and calling atoi, when there's no guarantee that each of the characters is convertible. #include <iostream>
#include <sstream>
#include <string>
int main (int argc, char * const argv[])
{
int myInt;
std::string aString = "3092";
char cString [] = "2903";
// Use a C string
std::stringstream withCString (aString.c_str ());
// Use another C string
std::stringstream withCString2 (cString);
// Use a string-class string
std::stringstream withString (aString);
withCString >> myInt;
std::cout << myInt << std::endl;
withCString2 >> myInt;
std::cout << myInt << std::endl;
withString >> myInt;
std::cout << myInt << std::endl;;
return 0;
}Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
|
|
#9 | ||
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 215
Rep Power: 3
![]() |
Quote:
Quote:
![]() |
||
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I think you're not paying attention to the responses. Next question?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 9:44 PM |
| Array issues :( | Alo Tsum | Java | 10 | Nov 26th, 2005 6:45 PM |
| Convert a string to unicode string | sma_soft | Delphi | 3 | Nov 16th, 2005 6:06 AM |
| replace space with ' * ' | TecBrain | C++ | 15 | Apr 13th, 2005 1:32 PM |
| function | solomon_13000 | Java | 6 | Apr 3rd, 2005 12:42 AM |