Solved

Converting all lowercase characters to uppercase

Posted on 2001-07-20
15
261 Views
Last Modified: 2012-06-22
How would I convert all of the lower case characters in a file to uppercase characters after the file has been copied? I know how to change the characters in a sentence, but how would I do it for a whole file?


void lower_case(char *);

void main(void)


    {
   

        void lower_case(char *line)


            {
                 int len = 0,i;
                 len = strlen(line);


                     for(i=0;i < len;i++){
                          if(line[i] >= 'A' && line[i] <= 'Z')
                               line[i] = line[i] + 32;
                     }
                     printf("lower case - %s\n\n",line);
            }/*end of lower_case function*/

            //converts sentence to uppercase
0
Comment
Question by:Michelle21221
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 2

Expert Comment

by:iDeb
ID: 6302698
you can try using _strupr and _strlwr
example from MSDN
/* STRLWR.C: This program uses _strlwr and _strupr to create
 * uppercase and lowercase copies of a mixed-case string.
 */

#include <string.h>
#include <stdio.h>

void main( void )
{
   char string[100] = "The String to End All Strings!";
   char *copy1, *copy2;
   copy1 = _strlwr( _strdup( string ) );
   copy2 = _strupr( _strdup( string ) );
   printf( "Mixed: %s\n", string );
   printf( "Lower: %s\n", copy1 );
   printf( "Upper: %s\n", copy2 );
}
0
 

Expert Comment

by:Mihnea
ID: 6302756
#include <iostream>
#include <string>

void main() {
  ifstream inStream;
  inStream.open( "Path to file", ios::in);

  if( !inStream ) return;

  string line;

  while( true ){
    getline( inStream, line );
    if( inStream.fail() ) return;  // no more lines to read

    lower_case( line.c_str() );
  }

  inStream.close();
}

void lower_case(char *line){
   int len = 0,i;
   len = strlen(line);

   for(i=0;i < len;i++){
       if(line[i] >= 'A' && line[i] <= 'Z')
           line[i] = line[i] + 32;
   }
   printf("lower case - %s\n\n",line);
}/*end of lower_case function*/
0
 
LVL 4

Accepted Solution

by:
AssafLavie earned 50 total points
ID: 6302939
Allow me to suggest a slightly more elegant solution (same stuff, more STL):

     ifstream fin("c:\\source.txt");
     ofstream fout("c:\\target.txt");
     assert(fout && fout);
     fin.unsetf(ios::skipws);
     transform(istream_iterator<char>(fin), istream_iterator<char>(), ostream_iterator<char>(fout), toupper);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:Mihnea
ID: 6302958
> assert(fout && fout);
You should not use an assert in this case, because you may not want the program execution to halt only because the file could not be opened
0
 

Expert Comment

by:Mihnea
ID: 6302964
AssafLavie nice and elegant solution otherwise.
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6302977
:)
Don't worry. assert was there just to clarify that error checking is required. Thanks for the compliment.
0
 

Author Comment

by:Michelle21221
ID: 6303326
Would this code work? To take a file copy all characters from input file stream inFile to output file stream outFile and convert all lowercase characters to uppercase. If not could someone tell me what the problem is?


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

int main()


    {    

    char ch;    
    ifstream fin;
    ofstream fout;
   
    while(c!=EOF)
    fout.open("inFile");
    fout.close();
    fin.open("inFile"); // Orginal file
    fout.open("outFile"); // Copy

    if( !fin || !fout )

        {
       
    }

    while( fin.get( ch ) )


        {
        fout << ch;
    }

void main()

 {

 ifstream inStream;
 inStream.open( "Path to file", ios::in);

 if( !inStream ) return;

 string line;

 while( true ){
   getline( inStream, line );
   if( inStream.fail() ) return;  // no more lines to read

   lower_case( line.c_str() );
 }

 inStream.close();
}

void lower_case(char *line){
  int len = 0,i;
  len = strlen(line);

  for(i=0;i < len;i++){
      if(line[i] >= 'A' && line[i] <= 'Z')
          line[i] = line[i] + 32;
  }
  printf("lower case - %s\n\n",line);
}/*end of lower_case function*/
           


    fin.close();
    fout.close();
   
    return 0;
}



0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6303354
A. Why not use simpler code that has already been tested many times (STL)?
B. You're using old-C++ headers <iostream.h>, you should use <iostream> instead (no .h).
C. You have two seperate main's here. What's that all about? Also, main should always return int, never void.
D. Your call to lower_case shouldn't compile since it has a char* parameter and you're passing it a const char* (from c_str()).
E. Your while loop for reading the content of the file line by line is ill-designed. Instead of while(true) and then breaking, use while(inStream).

Again, why not reuse existing code?
Don't write the loop, use an STL algorithm instead.
Don't write your own upper_case/lower_case functions, use STL's.
etc.
0
 

Expert Comment

by:Mihnea
ID: 6303382
AssafLavie
>E. Your while loop for reading the content of the file >line by line is ill-designed. Instead of while(true)
>and then breaking, use while(inStream).
The change you are suggesting would cause the last line to pe processed twice.

Michelle21221
You should use the sample code AssafLavie has provided but replacing the "assert(fout && fout);" with "if(!fin && !fout) return;"
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6303398
>>and then breaking, use while(inStream).
>The change you are suggesting would cause the last line to pe processed twice.
Yes, but saves you the cost of checking for the validity of the opened istream.
0
 

Expert Comment

by:sumansamanta
ID: 6304258
>>Don't write the loop, use an STL algorithm instead.
>>Don't write your own upper_case/lower_case functions, >>use STL's.

Yes you can easily write a generic fuction that converts a given string to lower - case. The function goes like this ..

#include <iostream>
#include <string>   //For STL string class
#include <iterator> //For STL iterators
#include <cctype>   //For toupper(), tolower()
using namespace std;

template <class InputIterator>
inline InputIterator genericlower(InputIterator it1)
{
    InputIterator itTemp = it1;
    while( (*it1++) = tolower(*it1) );
    it1 = itTemp;
    return it1;
}

inline string strlower(string& str) {
    return genericlower(str.begin());
}

inline char* strlower(char* str) {
    return genericlower(str);
}


int main() {
    string str1 = "SUMAN";
    char str2[] = "SAMANTA";
    cout << strlower(str1) << " " << strlower(str2);
    cout << "\n";
    return 0;
}

so you can use this line to parse any given string ...
0
 

Expert Comment

by:sumansamanta
ID: 6304266
You can similarly write a generic funtion to convert a string to upper case, by making little change in the previous code only

template <class InputIterator>
inline InputIterator genericupper(InputIterator it1)
{
   InputIterator itTemp = it1;
   while( (*it1++) = toupper(*it1) );
   it1 = itTemp;
   return it1;
}

inline string strupper(string& str) {
   return genericupper(str.begin());
}

inline char* strupper(char* str) {
   return genericupper(str);
}

int main() {
   string str1 = "suman";
   char str2[] = "samanta";
   cout << strupper(str1) << " " << strupper(str2);
   cout << "\n";
   return 0;
}
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6304507
sumansamanta: Why write it when can use STL?
What's wrong with:
transform(s.begin(), s.end(), s.begin(), tolower);
?
Or ctype::tolower() ? (for char arrays)






0
 

Expert Comment

by:sumansamanta
ID: 6304564
Yes we can do that, this was just a custom implementation using STL iterators, which will work as well.
0
 
LVL 4

Expert Comment

by:AssafLavie
ID: 6304719
Actually, you have an extra copy in the return value (string). So it's indeed less efficient than the STL version.
And no, I don't consider it 'just as good' to write something myself when I know that I can **reuse** a reliable standard implementation.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IdTelnet1DataAvailable handler freezes the Application 4 93
Getting IP address 8 104
computer science syllabus 3 103
Need some help with mailto 16 34
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question