Solved

Converting all lowercase characters to uppercase

Posted on 2001-07-20
15
226 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
Comment Utility
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
Comment Utility
#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
Comment Utility
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
Comment Utility
> 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
Comment Utility
AssafLavie nice and elegant solution otherwise.
0
 
LVL 4

Expert Comment

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

Author Comment

by:Michelle21221
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Expert Comment

by:AssafLavie
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

10 Experts available now in Live!

Get 1:1 Help Now