Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,423 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,978 people online right now. Registration is fast and FREE... Join Now!




C++ C-string case conversion

 
Reply to this topicStart new topic

C++ C-string case conversion, Convert from a text file in ANSI format written in capital letters, to

Rating  4
JMFitz190
10 Jan, 2008 - 09:11 AM
Post #1

New D.I.C Head
*

Joined: 10 Jan, 2008
Posts: 5


My Contributions
OK So here I have a program that will convert my text file into CAPITAL letters. I was wondering if there is a way to convert back from CAPITAL to lower case letters.

I have the following:

CODE

//Chapter 8
//Step by Step 8.7
//convert.cpp

#include <fstream.h>
#include <iostream.h>
#include <iomanip.h>

int main()
{
    char ch;
    ifstream infile;
    ofstream outfile;
    
    infile.open("Lower.txt",ios::in);
    outfile.open("Upper.txt", ios::out);
    
    if ((!infile) || (!outfile))
    {
         cout << "There was an Error opening the file. \n";
    system("Pause");
    return 0;
    }
    
    infile.unsetf(ios::skipws);
    
    while (!infile.eof())
      {
          infile >> ch;
          if (!infile.eof())
            {
                if ((ch > 96) && (ch < 123))
                   {
                        ch = ch - a;
                   }
                outfile << ch;
            }
      }
      
    infile.close();
    outfile.close();
      
     system("Pause");
     return 0;
}    

Oh FYI-- there are no errors. this just converts from Lower to UPPER
and I created a text file with both UPPER and Lower cAsE LeTtERs.

This post has been edited by JMFitz190: 10 Jan, 2008 - 09:12 AM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C++ C-string Case Conversion
10 Jan, 2008 - 10:19 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,550



Thanked: 67 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
toupper
tolower
Both are defined in ctype.h
User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: C++ C-string Case Conversion
10 Jan, 2008 - 06:08 PM
Post #3

D.I.C Foot
Group Icon

Joined: 1 Aug, 2007
Posts: 985



Thanked: 2 times
Dream Kudos: 375
My Contributions
http://www.dreamincode.net/code/snippet39.htm

Next time, search first, maybe you can find.smile.gif
User is offlineProfile CardPM
+Quote Post

JMFitz190
RE: C++ C-string Case Conversion
11 Jan, 2008 - 08:56 AM
Post #4

New D.I.C Head
*

Joined: 10 Jan, 2008
Posts: 5


My Contributions
QUOTE(nirvanarupali @ 10 Jan, 2008 - 07:08 PM) *

http://www.dreamincode.net/code/snippet39.htm

Next time, search first, maybe you can find.smile.gif



I did search. No post had my issue. I'm converting an external text file with more than just one letter. I tried "toupper" and "tolower" That didn't work.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: C++ C-string Case Conversion
11 Jan, 2008 - 11:54 AM
Post #5

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Basically, you'll need to loop through the entire file, converting each character individually using toupper() or tolower as appropriate, and outputting them to a new temporary file. You won't be able to actually change the characters within the existing file; but if you delete the original and rename the temporary file once the operations are complete, it will amount to almost the same thing.
User is offlineProfile CardPM
+Quote Post

JMFitz190
RE: C++ C-string Case Conversion
14 Jan, 2008 - 08:53 AM
Post #6

New D.I.C Head
*

Joined: 10 Jan, 2008
Posts: 5


My Contributions
QUOTE(jjhaag @ 11 Jan, 2008 - 12:54 PM) *

Basically, you'll need to loop through the entire file, converting each character individually using toupper() or tolower as appropriate, and outputting them to a new temporary file. You won't be able to actually change the characters within the existing file; but if you delete the original and rename the temporary file once the operations are complete, it will amount to almost the same thing.


Oohhhh ok, Thank you. I wasn't sure how the "toupper" and "tolower" worked. I knew it would convert single letters at one time.
Thanks for all the help! I appreciate it.
J M Fitz
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:32AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month