saiz66 Posted August 5, 2004 Share Posted August 5, 2004 Hi. I have a string with various numbers and letters. The first has to be a letter and the second a number. eg. A5 I know how to get the number: int number; number = (string[1] - 48); I was just wondering if there was a better way? Link to comment Share on other sites More sharing options...
0 bwx Posted August 5, 2004 Share Posted August 5, 2004 Yes, what you're doing is fine, but it might be clearer if you do: number = (string[1] - '0'); If you want a cleaner way to do it, here's an example I put together: #include <iostream> #include <string> #include <vector> void getNumbers (const std::string& someString, std::vector<int>& v) { for(std::string::size_type i=0;i<someString.size();++i) if (someString[i] >= '0' && someString[i] <= '9') v.push_back(someString[i]-'0'); } int main (int argc, char **argv) { if (argc != 2) { std::cout << "usage: " << argv[0] << " <someString>\n"; return 0; } std::vector<int> v; getNumbers(argv[1], v); std::cout << "found " << v.size() << " numbers in " << argv[1] << "\n\n"; if (v.size() != 0) { std::cout << "The Numbers: "; for(std::vector<int>::size_type i=0;i<v.size();++i) { std::cout << v[i]; if (i != v.size()-1) std::cout << ", "; } std::cout << '\n'; } return 0; } If you have any questions about the code, just ask. :) Hope that helps. Link to comment Share on other sites More sharing options...
0 bithub Posted August 5, 2004 Share Posted August 5, 2004 int num = atoi(string+1); Link to comment Share on other sites More sharing options...
0 Genesi Posted August 5, 2004 Share Posted August 5, 2004 Does it need to be checked as the user types, or does the user type it in and then you check it? The first seems to be a bit more realistic. Let me know and I'll come up with something. Link to comment Share on other sites More sharing options...
0 saiz66 Posted August 6, 2004 Author Share Posted August 6, 2004 Thanks for all the replies. Basically I am getting a string of 3 digits. First is a letter, second is a number from 0-9 and third is also a number from 0-9. I gotta check that the first is a letter and the 2nd and 3rd are numbers. This is pretty easy. I just want to find an efficient way of doing it. To bwx: I am not sure what this means: number = (string[1] - '0'); Why are you subtracting it from 0? And sorry I haven't learnt vectors yet so I do not know what your code means. To bithub: int num = atoi(string+1); Does atoi change it to a number? To Genesi: The user types it in and then I check it. Link to comment Share on other sites More sharing options...
0 bwx Posted August 6, 2004 Share Posted August 6, 2004 (edited) To bwx: I am not sure what this means:? number = (string[1] - '0');? Why are you subtracting it from 0??? And sorry I haven't learnt vectors yet so I do not know what your code means. The value of '0' corresponds to the ASCII value 48. The reason you subtract 48 from characters to get the actual value is because numeric characters start from 48(0) and go up to 57(9) in ASCII encoding. So if you have a string of numbers, say "1234", to get the actual numbers stored in that string, you could subtract 48(0) from the ASCII value. I hope that made sens:huh:huh: As for vectors, they are just arrays with no set hard-limit. They expand as you add values to them. You could also write it with regular arrays, but that wouldn't be as easy as writing it with vectors: #include <iostream> #include <string> int main (int argc, char **argv) { if (argc != 2) { std::cout << "usage: " << argv[0] << " <someString>\n"; return 0; } std::string inString = argv[1]; int n = 0; // find the number of digits in the string std::string::size_type i=0; while (i<inString.size()) { if (inString[i] >= '0' && inString[i] <= '9') ++n; ++i; } std::cout << "found " << n << " numbers in \"" << inString << "\"\n\n"; if (n != 0) { int *numArray; try { numArray = new int[n]; } catch (std::bad_alloc) { std::cout << "out of memory\n"; return -1; } int slot = 0; std::string::size_type i=0; // find all the numbers and put them in the array while (i<inString.size()) { if (inString[i] >= '0' && inString[i] <= '9') numArray[slot++] = inString[i] - '0'; ++i; } // print the numbers std::cout << "The Numbers: "; int x =0; while (x<n) { std::cout << numArray[x]; // if it's not the last element in the array, print out a comma if (x != n-1) std::cout << ", "; ++x; } std::cout << '\n'; } return 0; } Hope that helps. Edited August 6, 2004 by bwx Link to comment Share on other sites More sharing options...
0 Genesi Posted August 6, 2004 Share Posted August 6, 2004 (edited) #include <iostream> ?//Input/Ouput #include <string> ?//Strings #include <cctype> ?//For Char Functions using namespace std; int main() { //Initiazlize a emtpy strings string code = " "; //User inters code; cin >> code; //This if statement checks 3 values that all must be true //That is, the first item must be a alpabet and the //second and third items entered must be a number. //Here again I access indiviual elements of the string //to check each one to make sure they match the //requirements. if ( isalpha(code[0]) && (isdigit(code[1])) && ? ? ( isdigit(code[2])) ) ? //This block will execute if the code above is true. { ?cout << "Code Confirmed"; } //This block will execute if the code above is false. else { ?cout << "Invalid Code!"; } //Terminates Program. return 0; } Shortest way I could think of -- of course even shorter without the comments. I hope you don't mind working with strings as opposed to char arrays. I just find them a lot easier to work with. Edited August 6, 2004 by Genesi Link to comment Share on other sites More sharing options...
0 saiz66 Posted August 6, 2004 Author Share Posted August 6, 2004 wow... i forgot all about isalpha and isdigit.. you are a genius Genesi... thanks to everybody! Link to comment Share on other sites More sharing options...
0 bithub Posted August 6, 2004 Share Posted August 6, 2004 To bithub:int num = atoi(string+1); Does atoi change it to a number? Yeah Link to comment Share on other sites More sharing options...
0 Genesi Posted August 6, 2004 Share Posted August 6, 2004 Nah no genius :blush: just like helping people with c++. Helping you helps me learning it better; it's a win-win situation :) To further improve on this lets say the first if is true. You then can make an pointer to strings, something like: string * mycodes, that contain a list of known valid codes. After the first if is true make a second if to compare the string to mycodes to see if it is a match. Main thing I hate about programming is error checking makes programs twice as long Link to comment Share on other sites More sharing options...
Question
saiz66
Hi. I have a string with various numbers and letters. The first has to be a letter and the second a number. eg. A5 I know how to get the number:
int number;
number = (string[1] - 48);
I was just wondering if there was a better way?
Link to comment
Share on other sites
9 answers to this question
Recommended Posts