Replace AlphaNumeric Characters

This is a discussion on Replace AlphaNumeric Characters within the C++ Programming forums, part of the General Programming Boards category; I'm doing a prog that take the user input and prefixes each alpa numeric character and the '.' character, with ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Replace AlphaNumeric Characters

    I'm doing a prog that take the user input and prefixes each alpa numeric character and the '.' character, with "*jl" otherwise it leaves it alone.
    It works, the problem is it doesn't do it for uppercase, how I can modifly it so that it converts the user input cin to all lower case?

    cin.get().toLowerCase() isn't working, I saw that somewhere...

    Code:
     
    #include <iostream>
     
    #include <iomanip>
     
    #include <string>
     
    #include <algorithm>
     
    using std::cout; 
     
    using std::string; using std::cin;
     
    string alphaNumericData = "abcdefghijklmnopqrstuvwxyz.0123456789";
     
    string e = "*jl";
     
    int main()
     
    {
     
    char ch;
     
    while ( (ch = cin.get()) != EOF){
     
    unsignedint loc = alphaNumericData.find( ch, 0 );
     
    if( loc != string::npos )
     
    cout << e + ch;
     
    else cout << ch; 
     
    }
     
    return 0;
     
    }
     
    

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    There's tolower function...you can make use of it....
    Prototype:
    int tolower( int c );
    header file required
    <cstdlib> or <ctype>

  3. #3
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >the problem is it doesn't do it for uppercase
    That's because your search string doesn't include upper case letters. You can add them manually like you did with the lower case letters, or use tolower as mentioned above. Alternatively, the cctype header also declares isalnum, which does exactly what you want and you can get rid of the search string entirely, and the program will very likely be faster because the is* functions typically use a table lookup instead of a sequential search:
    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    
    namespace {
      const std::string repl ( "*jl" );
    }
    
    int main()
    {
      std::string s;
    
      while ( getline ( std::cin, s ) ) {
        std::string::iterator i = s.begin();
        std::string result;
    
        while ( i != s.end() ) {
          if ( isalnum ( *i ) )
            result += repl;
          result.push_back ( *i++ );
        }
    
        std::cout << s << '\n';
        std::cout << result << '\n';
      }
    }

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    well what I didn't wasn't efficient, but it works. I put in

    Code:
    while ( (ch = cin.get()) != EOF){
    ch = tolower(ch); 
    unsignedint loc = alphaNumericData.find( ch, 0 );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. How to replace strings in a file?
    By Doh in forum C Programming
    Replies: 6
    Last Post: 11-26-2002, 11:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21