changing the case of a string

This is a discussion on changing the case of a string within the C++ Programming forums, part of the General Programming Boards category; what i'm trying to do is take the input from a file and change all the upper case letters to ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    changing the case of a string

    what i'm trying to do is take the input from a file and change all the upper case letters to lower case and lower case letters to upper....now what i have so far does this but it doesn't keep the spaces from file it all just gets put on one line....

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    void changecase(ifstream&, ofstream&);
    
    int main()
    {
      ifstream infile;
      ofstream outfile;
      infile.open("textforlab9");
      outfile.open("changedtextforlab9");
    
      changecase(infile, outfile);
    
      infile.close();
      outfile.close();
    
      return 0;
    }
    
    void changecase(ifstream& in, ofstream& out)
    {
      char ch;
      while (!in.eof())
        {
          in >> ch;
          if (ch > 'A' && ch < 'Z')
            {
              ch = tolower(ch);
              out << ch;
            }
          if (ch > 'a' && ch < 'z')
            {
              ch = toupper(ch);
              out << ch;
            }
        }
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
          if (ch > 'A' && ch < 'Z')
            {
              ch = tolower(ch);
              out << ch;
            }
          if (ch > 'a' && ch < 'z')
            {
              ch = toupper(ch);
              out << ch;
            }
    maybe should be
    Code:
    if (ch > 'A' && ch < 'Z')
    {
    	ch = tolower(ch);
    }
    else if (ch > 'a' && ch < 'z')
    {
    	ch = toupper(ch);
    }
    
    out << ch;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    And you should use >= and <=, or A and Z won't get switched.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    And you should use >= and <=, or A and Z won't get switched
    or even use islower and isupper functions for portability
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Even better, yeah. Not to mention that islower and isupper could handle non-ASCII characters.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    ahh, but it's still outputting everything without spaces

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    That's because the streamextractors >> skip whitspace. you should use get().
    Your while condition is wrong as well. Your way you would process the last character twice.
    the preferred way woud be
    Code:
      char ch;
      while ( in.get( ch ) ) {  // while no error reading from in ( eof is an error condition )
        ....
    Kurt

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    could you explain the getline() function for me or show me how i'd use it in my situation because i've been reading over the syntax and i dont think i really understand how i can do the getline (which is for strings right?) and then read in a char from it =\

    also...i just noticed it's cutting off a bunch of letters in the middle of the string

    kurt: with the way you have the while statement it did stop processing the last character twice...but now it doesn't process the 1st character


    oh yeah, here's what the function looks like now
    Code:
    void changecase(ifstream& in, ofstream& out)
    {
      char ch;
      while (in.get(ch))
        {
          in >> ch;
          if (isupper(ch))
            {
              ch = tolower(ch);
            }
          else if (islower(ch))
            {
              ch = toupper(ch);
            }
          out << ch;
          //cout << ch;
    
        }
      // cout << endl;
    }
    Last edited by ammochck21; 11-27-2006 at 11:09 AM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You should use the get function, not getline. The get function as shown reads in each and every character, including whitespace. ZuK's example should be all you need to get going.

    You don't really want the getline function either, because it discards newlines. You could use it if you wanted, but that would take extra code to read in the line and then loop through each character in the string.

    One other option is to keep your original code but to use the noskipws manipulator to stop operator>> from skipping whitespace. I think get() is simpler, though.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    ahh daved thanks for the noskipws advice it's working perfectly now

  11. #11
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by ammochck21
    kurt: with the way you have the while statement it did stop processing the last character twice...but now it doesn't process the 1st character
    Code:
    void changecase(ifstream& in, ofstream& out)
    {
      char ch;
      while (in.get(ch))
        {
    //      in >> ch;          // you already read the char in the while condition.
                               //  must be removed
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 09:18 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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