How to check the ASCII value ??
-
Hello All, I am trying a read text file and extracting words from it and adding to a map. I want to add only valid words and eliminate numbers and special character words and then add it to map. Invalid words like 1word , hello# , t2o etc … so I want to eliminate all the number and special characters. Can anyone please help me with this …. strlwr(word); wsize = strlen(word); stemp = word; for(int j =0; j<=wsize; j++) { if ( stemp[j] == '.' || stemp[j] == ';' || stemp[j] == '*' || stemp[j] == '#' && stemp[j] == '!' || stemp[j] == '@' || stemp[j] == '$' || stemp[j] == '%' && stemp[j] == '^' || stemp[j] == '&' || stemp[j] == '(' || stemp[j] == ')' && stemp[j] == '-' || stemp[j] == '+' || stemp[j] == '/' ) break; else FMap[stemp]=offset; } This conditions are very lengthy, is there any way I can reduce it by checking the ASCII value and add them to MAP. Thanking you, Suresh HC.
-
Hello All, I am trying a read text file and extracting words from it and adding to a map. I want to add only valid words and eliminate numbers and special character words and then add it to map. Invalid words like 1word , hello# , t2o etc … so I want to eliminate all the number and special characters. Can anyone please help me with this …. strlwr(word); wsize = strlen(word); stemp = word; for(int j =0; j<=wsize; j++) { if ( stemp[j] == '.' || stemp[j] == ';' || stemp[j] == '*' || stemp[j] == '#' && stemp[j] == '!' || stemp[j] == '@' || stemp[j] == '$' || stemp[j] == '%' && stemp[j] == '^' || stemp[j] == '&' || stemp[j] == '(' || stemp[j] == ')' && stemp[j] == '-' || stemp[j] == '+' || stemp[j] == '/' ) break; else FMap[stemp]=offset; } This conditions are very lengthy, is there any way I can reduce it by checking the ASCII value and add them to MAP. Thanking you, Suresh HC.
Why don't you simply check if the value is between 'a' and 'z' or between 'A' and 'Z' ?
Cédric Moonen Software developer
Charting control [v1.1] -
Why don't you simply check if the value is between 'a' and 'z' or between 'A' and 'Z' ?
Cédric Moonen Software developer
Charting control [v1.1] -
Hello All, I am trying a read text file and extracting words from it and adding to a map. I want to add only valid words and eliminate numbers and special character words and then add it to map. Invalid words like 1word , hello# , t2o etc … so I want to eliminate all the number and special characters. Can anyone please help me with this …. strlwr(word); wsize = strlen(word); stemp = word; for(int j =0; j<=wsize; j++) { if ( stemp[j] == '.' || stemp[j] == ';' || stemp[j] == '*' || stemp[j] == '#' && stemp[j] == '!' || stemp[j] == '@' || stemp[j] == '$' || stemp[j] == '%' && stemp[j] == '^' || stemp[j] == '&' || stemp[j] == '(' || stemp[j] == ')' && stemp[j] == '-' || stemp[j] == '+' || stemp[j] == '/' ) break; else FMap[stemp]=offset; } This conditions are very lengthy, is there any way I can reduce it by checking the ASCII value and add them to MAP. Thanking you, Suresh HC.
You can use, isalpha() API or You can check the chars are alphabet or not.
if (((stemp[j] >= 65) && (stemp[j] <= 90) ||
((stemp[j] >= 97) && (stemp[j] <= 122)))
/* Alphabet (A-Z) or (a-z) */
else
/* Special or Numeric */
Do your Duty and Don't expect the Result
Rate this Post, if I helped You -
Thanks Cedric Moonen for the response. ASCII values A – 65 to Z – 90 & a – 97 to z – 122. But hw to check them in the condition ???
-
Hello All, I am trying a read text file and extracting words from it and adding to a map. I want to add only valid words and eliminate numbers and special character words and then add it to map. Invalid words like 1word , hello# , t2o etc … so I want to eliminate all the number and special characters. Can anyone please help me with this …. strlwr(word); wsize = strlen(word); stemp = word; for(int j =0; j<=wsize; j++) { if ( stemp[j] == '.' || stemp[j] == ';' || stemp[j] == '*' || stemp[j] == '#' && stemp[j] == '!' || stemp[j] == '@' || stemp[j] == '$' || stemp[j] == '%' && stemp[j] == '^' || stemp[j] == '&' || stemp[j] == '(' || stemp[j] == ')' && stemp[j] == '-' || stemp[j] == '+' || stemp[j] == '/' ) break; else FMap[stemp]=offset; } This conditions are very lengthy, is there any way I can reduce it by checking the ASCII value and add them to MAP. Thanking you, Suresh HC.
Suresh H wrote:
for(int j =0; j<=wsize; j++) {
One correction need to be done here, It should be modified to,
for(int j =0; j < wsize; j++)
. And use
isalpha
to identify character. You code should look like this,for(int j =0; j < wsize; j++)
{
if (isalpha(stemp[j]))
{
FMap[stemp] = offset;
}
}Prasad Notifier using ATL | Operator new[],delete[][^]
-
You can use, isalpha() API or You can check the chars are alphabet or not.
if (((stemp[j] >= 65) && (stemp[j] <= 90) ||
((stemp[j] >= 97) && (stemp[j] <= 122)))
/* Alphabet (A-Z) or (a-z) */
else
/* Special or Numeric */
Do your Duty and Don't expect the Result
Rate this Post, if I helped YouHi Appu, Thanks for the response, I made changes but the code as no effect its adding all the words. Including numbers and special characters.
for(int j =0; j<=wsize; j++) { if ( (stemp[j] >= 65) && (stemp[j] <= 90) || (stemp[j] >= 97) && (stemp[j] <= 122) ) { FMap[stemp]=offset; } } }
Something is wrong in the for loop i am unable to find, what will the cause ? -
Suresh H wrote:
for(int j =0; j<=wsize; j++) {
One correction need to be done here, It should be modified to,
for(int j =0; j < wsize; j++)
. And use
isalpha
to identify character. You code should look like this,for(int j =0; j < wsize; j++)
{
if (isalpha(stemp[j]))
{
FMap[stemp] = offset;
}
}Prasad Notifier using ATL | Operator new[],delete[][^]
Hi Prasad, Code as no effect its adding all the words.
void getoff(char *fname) { ifstream fin; fin.open(fname,ios::in); int wsize; string stemp; //Loops till the end of the file. while(!fin.eof()) { fin >> word; strlwr(word); wsize = strlen(word); offset=fin.tellg(); offset = offset - wsize + 1; //Insert Words and its offset in to Map stemp = word; for(int j =0; j < wsize; j++) { if (isalpha(stemp[j])) { FMap[stemp] = offset; } } } fin.close(); }
-
Hi Appu, Thanks for the response, I made changes but the code as no effect its adding all the words. Including numbers and special characters.
for(int j =0; j<=wsize; j++) { if ( (stemp[j] >= 65) && (stemp[j] <= 90) || (stemp[j] >= 97) && (stemp[j] <= 122) ) { FMap[stemp]=offset; } } }
Something is wrong in the for loop i am unable to find, what will the cause ?Suresh H wrote:
if ( (stemp[j] >= 65) && (stemp[j] <= 90) || (stemp[j] >= 97) && (stemp[j] <= 122) )
you missed out one bracket and it means the whole condition is wrong
if ( ((stemp[j] >= 65) && (stemp[j] <= 90) )||
((stemp[j] >= 97) && (stemp[j] <= 122)) )
Do your Duty and Don't expect the Result
Rate this Post, if I helped You -
Suresh H wrote:
if ( (stemp[j] >= 65) && (stemp[j] <= 90) || (stemp[j] >= 97) && (stemp[j] <= 122) )
you missed out one bracket and it means the whole condition is wrong
if ( ((stemp[j] >= 65) && (stemp[j] <= 90) )||
((stemp[j] >= 97) && (stemp[j] <= 122)) )
Do your Duty and Don't expect the Result
Rate this Post, if I helped You -
i checked it and added brackets but still no effect same output
for(int j =0; j<=wsize; j++) { if ( ((stemp[j] >= 65) && (stemp[j] <= 90) || (stemp[j] >= 97) && (stemp[j] <= 122) ) ) { FMap[stemp]=offset; } }
Just copy paste the code, 'coz u r still missing the brackets,
if ( ((stemp[j] >= 65) && (stemp[j] <= 90)) ||
((stemp[j] >= 97) && (stemp[j] <= 122) ) )the condition is this, Char should be between A-Z (first &&) or (||) Char should be between a-z (second &&) that is,
if ((&&) || (&&))
Do your Duty and Don't expect the Result
Rate this Post, if I helped You -
Just copy paste the code, 'coz u r still missing the brackets,
if ( ((stemp[j] >= 65) && (stemp[j] <= 90)) ||
((stemp[j] >= 97) && (stemp[j] <= 122) ) )the condition is this, Char should be between A-Z (first &&) or (||) Char should be between a-z (second &&) that is,
if ((&&) || (&&))
Do your Duty and Don't expect the Result
Rate this Post, if I helped You -
Hi Prasad, Code as no effect its adding all the words.
void getoff(char *fname) { ifstream fin; fin.open(fname,ios::in); int wsize; string stemp; //Loops till the end of the file. while(!fin.eof()) { fin >> word; strlwr(word); wsize = strlen(word); offset=fin.tellg(); offset = offset - wsize + 1; //Insert Words and its offset in to Map stemp = word; for(int j =0; j < wsize; j++) { if (isalpha(stemp[j])) { FMap[stemp] = offset; } } } fin.close(); }
Here change the for loop as below,
int nIndex = 0;
for (int j = 0; j < wsize; j++)
{
if (isalpha(stemp[j]))
{
FMap[nIndex++] = stemp[j]; /* Not FMap[stemp] = offset */
}
}
FMap[nIndex] = '\0'; /* NULL termination */
Do your Duty and Don't expect the Result
Rate this Post, if I helped You -
I copied and pasted but still nt working :( again the same output
for(int j =0; j<=wsize; j++) { if ( ((stemp[j] >= 65) && (stemp[j] <= 90)) || ((stemp[j] >= 97) && (stemp[j] <= 122)) ) { FMap[stemp]=offset; } }
your condition checking is correct but the logic inside the loop is wrong, i just posted the reply, or refer below
int nIndex = 0;
for (int j = 0; j < wsize; j++) /* Not j <= wsize */
{
if (((stemp[j] >= 65) && (stemp[j] <= 90)) ||
((stemp[j] >= 97) && (stemp[j] <= 122)))
{
FMap[nIndex++] = stemp[j]; /* Not FMap[stemp] = offset */
}
}
FMap[nIndex] = '\0'; /* NULL termination */
Do your Duty and Don't expect the Result
Rate this Post, if I helped You -
your condition checking is correct but the logic inside the loop is wrong, i just posted the reply, or refer below
int nIndex = 0;
for (int j = 0; j < wsize; j++) /* Not j <= wsize */
{
if (((stemp[j] >= 65) && (stemp[j] <= 90)) ||
((stemp[j] >= 97) && (stemp[j] <= 122)))
{
FMap[nIndex++] = stemp[j]; /* Not FMap[stemp] = offset */
}
}
FMap[nIndex] = '\0'; /* NULL termination */
Do your Duty and Don't expect the Result
Rate this Post, if I helped Youappu i made changes but i am getting error for this lines FMap[nIndex++] = stemp[j]; /* Not FMap[stemp] = offset */ error C2679: binary '[' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion) FMap[nIndex] = '\0'; /* NULL termination */ error C2679: binary '[' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion) Now the problem with adding values to Map.
-
appu i made changes but i am getting error for this lines FMap[nIndex++] = stemp[j]; /* Not FMap[stemp] = offset */ error C2679: binary '[' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion) FMap[nIndex] = '\0'; /* NULL termination */ error C2679: binary '[' : no operator defined which takes a right-hand operand of type 'int' (or there is no acceptable conversion) Now the problem with adding values to Map.
Is FMap char[] or what? :confused: I don't understand what you are trying to do inside the loop, if FMAp is not a char[] or char* ?
Do your Duty and Don't expect the Result
Rate this Post, if I helped You -
Is FMap char[] or what? :confused: I don't understand what you are trying to do inside the loop, if FMAp is not a char[] or char* ?
Do your Duty and Don't expect the Result
Rate this Post, if I helped You -
Thanks Cedric Moonen for the response. ASCII values A – 65 to Z – 90 & a – 97 to z – 122. But hw to check them in the condition ???
Suresh H wrote:
SCII values A – 65 to Z – 90 & a – 97 to z – 122.
IsCharAlpha
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
Hi Prasad, Code as no effect its adding all the words.
void getoff(char *fname) { ifstream fin; fin.open(fname,ios::in); int wsize; string stemp; //Loops till the end of the file. while(!fin.eof()) { fin >> word; strlwr(word); wsize = strlen(word); offset=fin.tellg(); offset = offset - wsize + 1; //Insert Words and its offset in to Map stemp = word; for(int j =0; j < wsize; j++) { if (isalpha(stemp[j])) { FMap[stemp] = offset; } } } fin.close(); }
Look carefully at
for(int j =0; j < wsize; j++)
{
if (isalpha(stemp[j]))
{
FMap[stemp] = offset;
}
}Do you really want to add an entry to FMap every time you find an alpha character, or everytime you find a word with ALL the characters alpha?
Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
-
using namespace std; typedef map FileMap; #define SIZE 255 char word[SIZE]; int offset; FileMap FMap; i forgot to enable Ignore HTML tags in this message (good for code snippets) -- modified at 4:30 Friday 30th March, 2007
So FMap is a map to hold string and int, and you want to store words which has only alphates. If this is the senario you are not supposed to insert an item in the map for every charecter checking. Ok here are my questions? 1) How you are diffentiating the words 2) Do you want to remove the special and numeric values and store the string? or totally discarding the string? 3) Is the int value in the needed, whts it going to do if you just want to store the alpha words? -- modified at 5:22 Friday 30th March, 2007 Here is the idea to proceed, 1)Read a word from the file 2)check for any special or numeric chars in the word, if you found any break the loop and goto step 4. 3)If all the chars are alpha then insert in the map 4)continue the above steps untill the end All remaing is only the logical part, think and proceed. -- modified at 5:25 Friday 30th March, 2007
Do your Duty and Don't expect the Result
Rate this Post, if I helped You