Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to check the ASCII value ??

How to check the ASCII value ??

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
33 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suresh H
    wrote on last edited by
    #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.

    C P P D 4 Replies Last reply
    0
    • S Suresh H

      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.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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]

      S 1 Reply Last reply
      0
      • C Cedric Moonen

        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]

        S Offline
        S Offline
        Suresh H
        wrote on last edited by
        #3

        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 ???

        N T 2 Replies Last reply
        0
        • S Suresh H

          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.

          P Offline
          P Offline
          Parthi_Appu
          wrote on last edited by
          #4

          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

          S T 2 Replies Last reply
          0
          • S Suresh H

            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 ???

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            if ( stemp[j] >= 'a' && stemp[j]<= 'z' || stemp[j] >= 'A' && stemp[j]<= 'Z' ) { // add to map }

            nave

            1 Reply Last reply
            0
            • S Suresh H

              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.

              P Offline
              P Offline
              prasad_som
              wrote on last edited by
              #6

              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[][^]

              S 1 Reply Last reply
              0
              • P Parthi_Appu

                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

                S Offline
                S Offline
                Suresh H
                wrote on last edited by
                #7

                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 ?

                P 1 Reply Last reply
                0
                • P prasad_som

                  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[][^]

                  S Offline
                  S Offline
                  Suresh H
                  wrote on last edited by
                  #8

                  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(); }

                  P C 2 Replies Last reply
                  0
                  • S Suresh H

                    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 ?

                    P Offline
                    P Offline
                    Parthi_Appu
                    wrote on last edited by
                    #9

                    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

                    S 1 Reply Last reply
                    0
                    • P Parthi_Appu

                      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

                      S Offline
                      S Offline
                      Suresh H
                      wrote on last edited by
                      #10

                      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; } }

                      P 1 Reply Last reply
                      0
                      • S Suresh H

                        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; } }

                        P Offline
                        P Offline
                        Parthi_Appu
                        wrote on last edited by
                        #11

                        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

                        S 1 Reply Last reply
                        0
                        • P Parthi_Appu

                          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

                          S Offline
                          S Offline
                          Suresh H
                          wrote on last edited by
                          #12

                          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; } }

                          P 1 Reply Last reply
                          0
                          • S Suresh H

                            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(); }

                            P Offline
                            P Offline
                            Parthi_Appu
                            wrote on last edited by
                            #13

                            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

                            1 Reply Last reply
                            0
                            • S Suresh H

                              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; } }

                              P Offline
                              P Offline
                              Parthi_Appu
                              wrote on last edited by
                              #14

                              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

                              S 1 Reply Last reply
                              0
                              • P Parthi_Appu

                                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

                                S Offline
                                S Offline
                                Suresh H
                                wrote on last edited by
                                #15

                                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.

                                P 1 Reply Last reply
                                0
                                • S Suresh H

                                  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.

                                  P Offline
                                  P Offline
                                  Parthi_Appu
                                  wrote on last edited by
                                  #16

                                  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

                                  S 1 Reply Last reply
                                  0
                                  • P Parthi_Appu

                                    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

                                    S Offline
                                    S Offline
                                    Suresh H
                                    wrote on last edited by
                                    #17

                                    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

                                    P 1 Reply Last reply
                                    0
                                    • S Suresh H

                                      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 ???

                                      T Offline
                                      T Offline
                                      ThatsAlok
                                      wrote on last edited by
                                      #18

                                      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

                                      1 Reply Last reply
                                      0
                                      • S Suresh H

                                        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(); }

                                        C Offline
                                        C Offline
                                        cp9876
                                        wrote on last edited by
                                        #19

                                        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."

                                        T 1 Reply Last reply
                                        0
                                        • S Suresh H

                                          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

                                          P Offline
                                          P Offline
                                          Parthi_Appu
                                          wrote on last edited by
                                          #20

                                          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

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups