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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. isalpha, isalnum

isalpha, isalnum

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 3 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.
  • N Offline
    N Offline
    nss
    wrote on last edited by
    #1

    1.These two are supposed to take int args: Yet I see them being used a as isalpha('t') etc, i.e we are giving it a char arg. Why is that? 2.I did:

    char r[256] ;
    int t;
    cout << " enter string ";
    char c = cin.peek();
    cin >> r;
    while (c != '\n')

    {
      c = cin.peek();
      if (isalpha(c))cin >> r;
    }
    

    cout << " tr" << r << endl;
    }

    but when I put in 'abc%def' i got back the identical thing, not 'abcdef' which is what I expected... 3.Also I wanted to weed out spl chars using isalnum so that an input like abd$ef would get fed in and come out like abdef: char s[256]; cin >>s; but isalnum wont work either... :confused::confused: thanks, ns

    M A 2 Replies Last reply
    0
    • N nss

      1.These two are supposed to take int args: Yet I see them being used a as isalpha('t') etc, i.e we are giving it a char arg. Why is that? 2.I did:

      char r[256] ;
      int t;
      cout << " enter string ";
      char c = cin.peek();
      cin >> r;
      while (c != '\n')

      {
        c = cin.peek();
        if (isalpha(c))cin >> r;
      }
      

      cout << " tr" << r << endl;
      }

      but when I put in 'abc%def' i got back the identical thing, not 'abcdef' which is what I expected... 3.Also I wanted to weed out spl chars using isalnum so that an input like abd$ef would get fed in and come out like abdef: char s[256]; cin >>s; but isalnum wont work either... :confused::confused: thanks, ns

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      nss wrote: These two are supposed to take int args: Yet I see them being used a as isalpha('t') The original C library has some, shall we say, "interesting" designs. One of them being that the character-oriented functions (most notably getchar()) actually operate on ints. This is because of EOF, which is a special value that cannot be a legal char value. So the parameter type is something that can hold all legal char values, plus EOF, and they chose int. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- You cannot truly appreciate Dilbert unless you've read it in the original Klingon.

      1 Reply Last reply
      0
      • N nss

        1.These two are supposed to take int args: Yet I see them being used a as isalpha('t') etc, i.e we are giving it a char arg. Why is that? 2.I did:

        char r[256] ;
        int t;
        cout << " enter string ";
        char c = cin.peek();
        cin >> r;
        while (c != '\n')

        {
          c = cin.peek();
          if (isalpha(c))cin >> r;
        }
        

        cout << " tr" << r << endl;
        }

        but when I put in 'abc%def' i got back the identical thing, not 'abcdef' which is what I expected... 3.Also I wanted to weed out spl chars using isalnum so that an input like abd$ef would get fed in and come out like abdef: char s[256]; cin >>s; but isalnum wont work either... :confused::confused: thanks, ns

        A Offline
        A Offline
        Antti Keskinen
        wrote on last edited by
        #3

        As an answer to the first reply: I wouldn't say 'interesting'. I find it extremely useful that I can use both character symbols and integers to a character stream. Using an integer variable with a character conversion gives me access to symbols that are not normally available in the C/C++ coding syntax, such as scandinavian characters. Now, for the other questions. Firstly, you should consider using some other stream input function. Read the entire input string in first, then go through the string, removing all invalid characters. The problem with your function above is that when you peek the character, and then test it in against the alphanumeric conversion, you don't explicitly convert the character into an integer. Add an (int) conversion operator before c. This might solve the problem, but an even better implementation is to read the entire string first. What happens now is that you peek the character, and if it is alphanumeric, it is read into the input stream. If it is not alphanumeric, the character is left into the input stream, and you peek for the next character. As it happens now, the next character is considered alphanumeric, and the cin >> operation executes, and it actually reads everything from the input stream into the buffer, because r has lots of space available. Here is an alternative implementation that is a bit closer on what you are trying to accomplish:

        char buffer[256];
        char c;
        int nIndex = 0;

        cout << "Enter string: ";

        while (c != '\n')
        {
        cin >> c; // Alternatively, you could use _getch or cin.read( &c, 1)
        if ( isalpha( (int)c ) )
        {
        buffer[nIndex] = c;
        nIndex++;
        }
        }

        Not sure if that will work, but test it out & debug if needed.. But you should get the big picture :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

        N 1 Reply Last reply
        0
        • A Antti Keskinen

          As an answer to the first reply: I wouldn't say 'interesting'. I find it extremely useful that I can use both character symbols and integers to a character stream. Using an integer variable with a character conversion gives me access to symbols that are not normally available in the C/C++ coding syntax, such as scandinavian characters. Now, for the other questions. Firstly, you should consider using some other stream input function. Read the entire input string in first, then go through the string, removing all invalid characters. The problem with your function above is that when you peek the character, and then test it in against the alphanumeric conversion, you don't explicitly convert the character into an integer. Add an (int) conversion operator before c. This might solve the problem, but an even better implementation is to read the entire string first. What happens now is that you peek the character, and if it is alphanumeric, it is read into the input stream. If it is not alphanumeric, the character is left into the input stream, and you peek for the next character. As it happens now, the next character is considered alphanumeric, and the cin >> operation executes, and it actually reads everything from the input stream into the buffer, because r has lots of space available. Here is an alternative implementation that is a bit closer on what you are trying to accomplish:

          char buffer[256];
          char c;
          int nIndex = 0;

          cout << "Enter string: ";

          while (c != '\n')
          {
          cin >> c; // Alternatively, you could use _getch or cin.read( &c, 1)
          if ( isalpha( (int)c ) )
          {
          buffer[nIndex] = c;
          nIndex++;
          }
          }

          Not sure if that will work, but test it out & debug if needed.. But you should get the big picture :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

          N Offline
          N Offline
          nss
          wrote on last edited by
          #4

          thanks!

          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