Thread: Ignoring puncuations in input stream

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    6

    Ignoring puncuations in input stream

    hello, this is my first post, nice to meet everyone

    I am taking input from the keyboard, and I want to ignore
    whitespace, punctuation and the case of letters, i.e. 'a' = 'A'

    I figured out the whitespace, but I can't figure out letter case and
    punctuation. Here is my code (my main function, which uses a stack
    and queue objects). The program is meant to determine whether or
    not a statement is a palindrome.

    Code:
    #include "stack.h"
    #include "queue.h"
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        bool isPalindrome = true;
        char ch, ch1, ch2;
        StackType s;
        QueueType q;
    
        cout << "Enter a statement: ";
    
        cin >> ch;
        while(!s.IsFull() && ch != '\n')
        {
            s.Push(ch);
            q.Enqueue(ch);
            if(cin.peek() != '\n')
                cin >> ch;
            else
                cin.get(ch);
        }
    
        while(isPalindrome && !s.IsEmpty())
        {
            q.Dequeue(ch1);
            ch2 = s.Top();
            s.Pop();
            isPalindrome = ch1 == ch2;
        }
    
        cout << endl;
    
        if(isPalindrome)
            cout << "Entered statement is a Palindrome\n";
        else
            cout << "Entered statement is not a Palindrome\n";
        return 0;
    }
    thanks!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Some useful things from working a bit with assembly:

    • To guarantee a char will be lowercase you can OR it with 0x20.
    • To guarantee a char will be uppercase, you can AND with 0xDF.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can't handle case and punctuation with the input stream, you have to read in the character first and then handle them.

    You can use tolower and/or toupper to change the case of a character. You can use ispunct to check to see if the character is punctuation and skip it if it is.

    >> Some useful things from working a bit with assembly
    Those might be useful for a code obfuscation contest, but for a real program I would use tolower and toupper.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    6
    thank you, everything works, but for the tolower function,
    it returns the ASCII value for the char. Is there any way I can
    change that value back to the char instead of an ASCII int?

    thanks again

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Simply assign it to a char variable and it will be converted correctly.

    If you want to avoid a warning, just use a cast. The C++ style cast is:
    Code:
    ch = static_cast<char>(::tolower(ch));

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    What's the difference between "::tolower" and "tolower"?
    Are they located in different libraries or namespaces?

    I just tried them out and they give different outputs for the following lines of code:
    Code:
    char c;
    ...
    cout << hex << tolower(c) << endl;
    cout << hex << ::tolower(c) << endl;
    The upper output is hexadecimal, the lower is plain decimal. Why is that?
    Last edited by cunnus88; 03-30-2007 at 11:33 PM.

  7. #7
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    What's the difference between "::tolower" and "tolower"?
    Depends on how your header files and namespace stuff is set up:
    Code:
    #include <ctype.h>
    
    // both are the same because tolower() is in the global namespace
    cout << hex << tolower(c) << endl;
    cout << hex << ::tolower(c) << endl;
    Code:
    #include <cctype>
    
    // both are wrong because tolower() is in the std namespace; might not compile
    cout << hex << tolower(c) << endl;
    cout << hex << ::tolower(c) << endl;
    Code:
    #include <cctype>
    
    using namespace std;
    
    // this one is right because it's defaulted to the std namespace
    cout << hex << tolower(c) << endl;
    
    // this one is not because tolower() is in the std namespace; might not compile
    cout << hex << ::tolower(c) << endl;

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    What's the difference being "defaulted" to the std namespace and being "in" the namespace?

    In other words, what does "::" do, when it doesn't have a scope modifier (as in class A::B)?

  9. #9
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    What's the difference being "defaulted" to the std namespace and being "in" the namespace?
    Nothing. "defaulted" means that you said 'using namespace std' or 'using std::tolower' and from then on every tolower() becomes std::tolower() by default.
    In other words, what does "::" do, when it doesn't have a scope modifier (as in class A::B)?
    It's explicitly saying that the thing is in the global namespace.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linking file stream with input file?
    By flamehead144 in forum C Programming
    Replies: 8
    Last Post: 02-07-2009, 08:55 PM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  3. Help loading a vector with input from input stream
    By Bnchs in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2006, 03:53 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM