Solved

Converting all lowercase characters to uppercase

Posted on 2001-07-20
15
274 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

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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

691 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