#1 Members - Reputation: 110
Posted 26 September 2012 - 08:57 PM
#3 Moderator* - Reputation: 5362
Posted 26 September 2012 - 11:45 PM
No need, as you can use operator[] to index characters in the string.Break the string into a series chars, C_str should do fine. Then cast every character to an unsigned int.
@OP: I'd use std::transform with std::toupper to convert the string to upper case, then I'd use std::remove_if to remove all the characters that weren't between 'A' and 'Z', and then all you have to do is subtract 'A' and add 1 to each character and you're good to go. Alternatively, I'd use std::remove_if with std::isalpha to get it to just alpha characters, and then use std::transform with a function that did std::toupper() - 'A'+ 1 in one pass. Once the string is just a collection of bytes that represent the right values for each character, I'd probably just output each character to a std::ostream, casting each char to an int and adding a period between numbers.
Something like that. How I'd actually do it kind of depends on the situation.
#4 Members - Reputation: 603
Posted 27 September 2012 - 02:10 AM
No need, as you can use operator[] to index characters in the string.
Break the string into a series chars, C_str should do fine. Then cast every character to an unsigned int.
@OP: I'd use std::transform with std::toupper to convert the string to upper case, then I'd use std::remove_if to remove all the characters that weren't between 'A' and 'Z', and then all you have to do is subtract 'A' and add 1 to each character and you're good to go. Alternatively, I'd use std::remove_if with std::isalpha to get it to just alpha characters, and then use std::transform with a function that did std::toupper() - 'A'+ 1 in one pass. Once the string is just a collection of bytes that represent the right values for each character, I'd probably just output each character to a std::ostream, casting each char to an int and adding a period between numbers.
Something like that. How I'd actually do it kind of depends on the situation.
I didn't realize he only wanted to do uppercase letters.
Guess I should stop thinking like a C programmer.
#5 Members - Reputation: 291
Posted 27 September 2012 - 06:40 PM
But you could also write your own conversions..
stringstream takes average 2,000,000 ms to convert int <-> string
itoa and atoi takes average 350,000 ms to convert.
Offtopic
my convertions takes around 87,000ms for int to string. And around 43,000ms for string to int.
Meaning self written conversions are more effective, if good written. You are the programmer, you decide all the errorchecking.
I don't have a single check of wrong input to increase all performance. But I'm the only programmer at my projects so I know that I don't need to enter wrong input.
Offtopic
~EngineProgrammer
#6 Crossbones+ - Reputation: 1443
Posted 27 September 2012 - 08:51 PM
strings are just a collection of numbers organized in a specific pattern such that each letter corrosponds to a particular number(see ASCII), your implementation is already flawed, you use letter offset's, but you don't account between caps/lower case letters, so, assuming that capitals would come after your lowercase letters, than H would be 26+7. i'm not sure what convention you want to use, but I don't see why you can't just use the ASCII system already in place, however if you do need to change the number representing the character then Cornstalk's already provided a pretty solid implementation of what to do.I've been trying to work out how to convert strings to integers, ex "Hello world" should become "7.5.12.12.15 23.15.17.12.4". How would I do this? I dont want source code, I just want a basic explanation so I can figure out exactly how to use it. Thanks guys
There are allot of standard functions to convert data types. stringstream, itoa, atoi, etc..
But you could also write your own conversions..
stringstream takes average 2,000,000 ms to convert int <-> string
itoa and atoi takes average 350,000 ms to convert.
Offtopic
my convertions takes around 87,000ms for int to string. And around 43,000ms for string to int.
Meaning self written conversions are more effective, if good written. You are the programmer, you decide all the errorchecking.
I don't have a single check of wrong input to increase all performance. But I'm the only programmer at my projects so I know that I don't need to enter wrong input.
Offtopic
~EngineProgrammer
I truthfully hope you are either working with incredibly long strings/70's era processors, or that you simply meant micro-seconds(generally represented as 'mus' or 'us'), and not milliseconds, still, 2 seconds is a long time even for the standard library, so i'm not really sure what your doing.
Edited by slicer4ever, 27 September 2012 - 08:51 PM.
#7 Members - Reputation: 1300
Posted 27 September 2012 - 10:07 PM
At no point should stringstream, atoi, etc. have anything to do with it.
#8 Marketplace Seller - Reputation: 8937
Posted 27 September 2012 - 10:36 PM
Quoted for truth - look at Cornstalk's post. The only place int to string (not vise-versa) is needed, is for putting the results in a new string if the OP desires.At no point should stringstream, atoi, etc. have anything to do with it.
I'd do it something like this:
Cast string to lowercase. (std::transform, and std::tolower)
Iterate over each char in string (just index into it)
I'd cast the char to an int
//Since ASCII lowercase letters begins at 97, and goes to 122, and space is 32...
if the int is 32
. . . .output a '-' to the result, if desired, or leave it as a space, or discard it.
if the int is over 122 or under 97
. . . .output a ? to the result, or discard that char
otherwise
. . . . Subtract 96, so 'a' becomes '1', and so on.
Edited by Servant of the Lord, 27 September 2012 - 10:36 PM.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal






