Solved

Converting all lowercase characters to uppercase

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 clear a vector as well as how to detect empty vectors in C++.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now