UPPER TO LOWERCASE

Hey the following code works in counting vowels consonants and spaces of a given file but how do i convert an uppercase letter to lower? This must be
a simple fix but how would i put this into my loop?
Thanks for the help!
Justin




// Include Section
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;

// Main Program
int main( )
{
// Constant Declarations

// Variable Declations
char data; // Contents of the file.
int countvow = 0; // Counts the vowels
int countcon = 0; // Counts the consonants.
int space = 0; // Counts the spaces.

// Output Identification
system("CLS");
cout << "In Class/Take Home #x by John Smith - "
<< "Example Program\n\n";

ifstream in_stream;

in_stream.open("I:\\test.txt");
if (in_stream.fail())
{
cout << "Input file opening failed.\n";
}
////////////////////////////////////////////
// The following code reads in the data of the text file and determines if
// the character is a vowel, consonant, or space.


in_stream.get(data); // Retrieves the file contents.

while (! in_stream.eof())
{
//if(isupper(data))
// tolower(data);

if((data=='a') ||(data=='e') ||(data=='i') ||(data=='o') ||(data=='u'))
{
countvow++;
}
else
{
if(data != ' ')
countcon++;
}
if(data==' ')
space++;

in_stream.get(data);


}

////////////////////////////////////////////








in_stream.close();
//////////////////////////////////////////// Print out results.
cout << setw(15) << "Vowels" << setw(15) << countvow << endl;
cout << setw(19) << "Consonants" << setw(12) << countcon << endl;
cout << setw(15) << "Spaces" << setw(15) << space << endl;









cout << "\n\nEnd Program.\n";

return 0;
}
i would just do:
1
2
3
4
string input;
for( int i = 0; i <= input.length(); i++)
    if (int(input[i]) >= 65 && int(input[i]) <= 90)
      input.replace(i, 1, 1, char(int(input[i]) + 32));   


i am not sure if this will work
Last edited on
I would recommend for portability reasons not doing that because it is heavily tied to ASCII.

1
2
3
string input;
for( string::iterator i = input.begin(); i != input.end(); ++i )
    *i = static_cast<char>( tolower( *i ) );


Unfortunately I think the cast is necessary because of tolower()'s stupid declaration.

Another way to do this is with the transform() algorithm.

a good old c-runtime function

1
2
3
4
5
#include <ctype>

...

data = tolower( data ); // no need to check if its upper 


Yeah, although tolower operates only on chars, so a loop is needed to transform an entire string.
I prefer transform():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>

std::string lowercase( const std::string& s )
  {
  std::string result( s );
  std::transform(
    result.begin(),
    result.end(),
    result.begin(),
    std::ptr_fun <int, int> ( std::tolower )
    );
  return result;
  }

#include <iostream>
#include <sstream>
#include <vector>

int main()
  {
  using namespace std;

  string users_name;
  cout << "Please enter your full name> ";
  getline( cin, users_name );

  vector <string> users_names;
  istringstream iss( lowercase( users_name ) );
  string name;
  while (getline( iss, name, ' ' ))
    {
    users_names.push_back( name );
    }

  string final_name;
  for (unsigned n = 0; n < (users_names.size() -1); n++)
    {
    final_name += users_names[ n ][ 0 ];
    }
  final_name += ' ';
  final_name += users_names.back();

  cout << "\nee cummings would write it as \"" << final_name << "\"\n";

  return 0;
  }

An example run:

D:\prog\foo> g++ -Wall -pedantic a.cpp

D:\prog\foo> a
Please enter your full name> John Jacob Jingleheimer-Schmidt

ee cummings would write it as "jj jingleheimer-schmidt"

D:\prog\foo>

Have fun!
Topic archived. No new replies allowed.