to uppercase without standard function

Jun 3, 2017 at 5:44pm
Hello,

I'm looking for something to read or maybe somebody explain how to convert from uppercase to lowercase but without standard library.

Thanks.
Jun 3, 2017 at 6:26pm
From uppercase to lower case, add decimal 32 to it
From lowercase to upper case subtract decimal 32 from it
Jun 3, 2017 at 7:01pm
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
#include <iostream>
#include <string>
using namespace std;

const string lower = "abcdefghijklmnopqrstuvwxyz";
const string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

//=====================================================================

char myToUpper( char c )
{
   int pos = lower.find( c );
   if ( pos != string::npos ) return upper[pos];
   else                       return c;
}

//=====================================================================

char myToLower( char c )
{
   int pos = upper.find( c );
   if ( pos != string::npos ) return lower[pos];
   else                       return c;
}

//=====================================================================

int main()
{
   string test;

   test = "Manchester United 2013";  for ( char c : test ) cout << myToLower( c );
   cout << '\n';
   test = "Manchester City 2014"  ;  for ( char c : test ) cout << myToUpper( c );
   cout << '\n';
}


or if std::string and its library functions are banned, then
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
#include <iostream>
using namespace std;

const char *lower = "abcdefghijklmnopqrstuvwxyz";
const char *upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

//=====================================================================

char myToUpper( char c )
{
   for ( int i = 0; i < 26; i++ ) if ( lower[i] == c ) return upper[i];
   return c;
}

//=====================================================================

char myToLower( char c )
{
   for ( int i = 0; i < 26; i++ ) if ( upper[i] == c ) return lower[i];
   return c;
}

//=====================================================================

int main()
{
   for ( char c : "Manchester United 2013" ) cout << myToLower( c );
   cout << '\n';
   for ( char c : "Manchester City 2014"   ) cout << myToUpper( c );
   cout << '\n';
}

Last edited on Jun 3, 2017 at 7:22pm
Jun 4, 2017 at 9:18am
how to convert from uppercase to lowercase
The question looks deceptively simple, because it wouldn’t be so easy to write a general code which manages those languages where not all lowercase and uppercase letters have a one to one match or where diacritics are moved to a side of the letter when it becomes uppercase.
Luckily Latin alphabet doesn’t suffer from problems like those, but I think it would have been better to specify whether it was the only one to be dealt with or not.
Topic archived. No new replies allowed.