Click to See Complete Forum and Search --> : Characters and numbers in a string
Dxxv
October 29th, 2002, 01:27 PM
Hi all!
Suppose I have a input char* tmp = "A12" and it'll match one of 3 cases:
if(the first char = A)
then store the value after A ( which is 12) into a integer var
else if( the first char = B)
then store the value after B into a integer variable
else if( the first char = C)
then store the value after C into a integer variable
else
//do nothing
Please help with the coding. Thanks in advance.
Dxxv
PaulWendt
October 29th, 2002, 01:41 PM
If you're definitely going to be working with c-style strings, I'd
do something like this:
char* temp = "A12";
int aValue = 0;
int bValue = 0;
int cValue = 0;
switch (temp[0])
{
case 'A':
aValue = atoi(&temp[1]);
break;
case 'B':
bValue = atoi(&temp[1]);
break;
case 'C':
cValue = atoi(&temp[1]);
break;
default: /* do nothing */
break;
}
You could even create a wrapper class [or struct] whose interface
might look something like this:
class Conversion
{
public:
Conversion();
enum eConversionType {A, B, C};
void setBuffer(const std::string& buf);
int getValue() const;
eConversionType getType() const;
private:
std::string m_buf;
}
You'd have to tinker with its interface to get what you want. You
could even forget about the class and just go with a function to
do your string conversion.
--Paul
galathaea
October 29th, 2002, 01:43 PM
The basic steps then would be
Get first char (tmp[0]) and check if alphabetical (isalpha)
if so, convert rest of string to number (atoi(&(tmp[1]), ...) or stream conversion or whatever you are comfortable with)
You did the basic outline in your original post, so I am not sure what you need help with. Is it the check (I showed isalpha, but there are other methods)? Or is it the conversion (I showed atoi, but again many ways...)? If you clarify, I can definitely help you out better.
[EDIT:] Posted late. And notice now that you want to check only a-c. Previous post should help.
Dxxv
October 30th, 2002, 11:11 AM
Thanks all.
I got this to work. However I've another complicated case I need your helps, please.
If I have an input char*var = "A12B3C4". A,B,C can be in any order. How do I break this string and store the value like the previous msg?
Thanks
Dxxv
crazycliffy
October 30th, 2002, 05:39 PM
Here is get the locations of where all the alpha characters are...
#include <iostream.h> //for cout
#include <iomanip.h> // for the endl function
#include <string.h> //string manup stuff
#include <ctype.h> //for the isalpha, atoi, or atol, or atof functions
void main( void )
{
char *var = "A12B3C4";
const int lenght = strlen(var);
int index = 0;
int counter = 0;
int letter[128]; //place to store addresses to the alpha letters.
cout << "var is " << var << endl;
cout << "length of var is " << strlen(var) << endl;
for (int i=0;i<strlen(var);++i) {
if (isalpha(var[i])) {
counter++;
letter[index++] = i;
}
}
cout << "address locations of all alpha letters" << endl;
for (int j = 0; j < counter; ++j) {
cout << "letter[" << j << "] = " << letter[j] << endl;
}
}
Once you get the locations, you can use the string.h function:
char *strncpy(const char *string1,const char *string2, size_t n) -- Copy first n characters of string2 to stringl
Use the alpha addresses locations and put it into the strncpy function, and extract the data and put it into a char *, or int variable.
I left out the above part because it is an string.h excercise.
You can then use PaulWends' suggestion and use mine to
get what you want..
I hope this helps.
crazycliffy
October 30th, 2002, 06:21 PM
Here's an example of what you could to with strncpy:
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
void main(void)
{
char *var = "A12B3C4";
char s[128] = "";
cout << "strncpy(s,var,3)=" << strncpy(s,var,3) << endl;
cout << "strncpy(s,var[3],3)=" << strncpy(s,&var[3],2) << endl;
cout << "strncpy(s,var[5],3)=" << strncpy(s,&var[5],2) << endl;
}
crazycliffy
October 30th, 2002, 06:22 PM
oops forgot to put the output:
strncpy(s,var,3)=A12
strncpy(s,var[3],3)=B32
strncpy(s,var[5],3)=C42
PaulWendt
October 30th, 2002, 07:39 PM
I didn't bother looking at crazycliffy's suggestions, but I couldn't
help but notice his usage of iostream.h and iomanip.h.
These files are part of an older version of the C++ standard and
this is not how they should be referenced nowadays. This usage
is deprecated and isn't guaranteed to work on other compilers [or
even on a future version of Microsoft's compiler].
So, my suggestion is to kick the habit now:
#include <string>
#include <iostream>
#include <iomanip>
#include <cstring> // for the string.h functions in C
// uncomment the following line to make things easy
// on yourself.
// using namespace std;
int main(int argc, char* argv[])
{
// you can choose to fully qualify the string
// name from the std namespace this way instead:
std::string temp;
return 0;
}
This isn't an attempt to nitpick or belittle; I'm just trying to help.
--Paul
debkumar
October 31st, 2002, 04:37 AM
Paul,
i am also used to stuff like
iostream.h and
iomanip.h ....
which u said is part of an older version of the C++ standard.
Can u pls let me know some tutorials/links where i can get to know how they are referenced nowadays.
Regards
Debkumar
PaulWendt
October 31st, 2002, 06:20 AM
I don't know of any links or anything; I can refer you to books,
though. Everything in the C++ Standard Library is in the std
namespace. Everything in the new header files [basically the
same as the old ones, but without the .h] need to be fully
qualified or you to use "using" in order to bring their name out.
If you have The C++ Programming Language, check out
chapter 16.1.2; Stroustrup put all of the header files in there.
--Paul
crazycliffy
October 31st, 2002, 12:03 PM
i apologize for the improper header usage.
i wasn't really thinking of a c++ solution,
but more of a quick suggestion, to the problem posted.
next time i'll put review future code and make
to Stroustrup standard.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.