Turn uppercase to lowercase

I want to turn uppercase to lowercase and vice versa.
Input:
Acesta EsTe un SIR
Output:
aCESTA eStE UN sir
but my code displays : aCESTA@eStE@UN@sir



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int main() {
   // ifstream fin("date.in");

char s[1001], s1[1001];
int i;
cin.get(s,1001);
int n = strlen(s);
for (i = 0; i < n; i++)
if (s[i] >= 'a' && s[i] <= 'z')
s[ i ]= s[ i ] - 32;
else
s[ i ]= s[ i ] + 32;
cout << s;
}
Last edited on
Change the "else" to
else if (s[i] >= 'A' && s[i] <= 'Z')
Hello bstroe,

Here is another option using C++ code:
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
#include <iostream>
#include <string>
#include <cctype>

//using namespace std;  // <--- Best not to use.

int main()
{
    // ifstream fin("date.in");

    std::string str1/*, str2*/;
    //int idx;  // <--- Should define in the for loop.

    std::cout << "\n Enter a string: ";  // <--- Change any way you like, but the input needs a prompt.
    std::getline(std::cin, str1);

    //int n = strlen(str1);

    for (size_t idx = 0; idx < str1.size(); idx++)
    {
        if (std::isupper(str1[idx]))
            str1[idx] = std::tolower(str1[idx]);
        else if (std::islower(str1[idx]))
            str1[idx] = std::toupper(str1[idx]);
    }

    std::cout << str1;

    return 0;  // <--- Not required, but makes a good break point.
}

Also if you need it use the C++ version "<cstring>" not "<string.h>".

If you intend to read from a file later add "<fstream>" back.

Andy
If you use tolower() in C++, then as has been pointed out previously, the argument and return value needs to be cast

 
str1[idx] = static_cast<char>(std::tolower(static_cast<unsigned char>(str1[idx])));


and similar for std::toupper(), isupper() etc.

Also, with C++ use range for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <cctype>

int main()
{
	std::string str1;

	std::cout << "Enter a string: ";
	std::getline(std::cin, str1);

	for (auto& ch : str1)
		if (std::isupper(static_cast<unsigned char>(ch)))
			ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
		else
			if (std::islower(static_cast<unsigned char>(ch)))
				ch = static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));

	std::cout << str1;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

int main()
{
   const string alphabet = "abcdefghijklmnopqrstuvwxyz";
   const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   const string aA = alphabet + ALPHABET, Aa = ALPHABET + alphabet;
   string test;
   cout << "Input a string: ";   getline( cin, test );
   for ( char c : test )
   {
      int pos = aA.find( c );
      cout << ( pos == string::npos ? c : Aa[pos] );
   }
   cout << '\n';
}


Input a string: Acesta ESTE un SIR
aCESTA este UN sir
Flipping a C string's case using iterators, an algorithm, a lambda and the ternary conditional operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cctype>    // std::islower, std::tolower, std::toupper
#include <algorithm> // std::transform, https://en.cppreference.com/w/cpp/algorithm/transform
#include <iterator>  // std::begin. std::end...requires C++11 or later

int main()
{
   const unsigned STRLEN { 100 };

   char cstr[STRLEN];

   std::cout << "Enter string to convert: ";
   std::cin.getline(cstr, STRLEN);
   std::cout << '\n';

   std::transform(std::begin(cstr), std::end(cstr), std::begin(cstr),
                  [] (char c) -> char
                     { return  std::islower(c) ? std::toupper(c) : std::tolower(c); });

   std::cout << cstr << '\n';
}

Enter string to convert: Acesta EsTe un SIR

aCESTA eStE UN sir

Transforming a C++ string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cctype>    // std::islower, std::tolower, std::toupper
#include <algorithm> // std::transform
#include <string>

int main()
{
   std::string str;

   std::cout << "Enter string to convert: ";
   std::getline(std::cin, str);
   std::cout << '\n';

   std::transform(str.begin(), str.end(), str.begin(),
                  [] (char c) -> char
                     { return  std::islower(c) ? std::toupper(c) : std::tolower(c); });

   std::cout << str << '\n';
}

Enter string to convert: Acesta EsTe un SIR

aCESTA eStE UN sir
Topic archived. No new replies allowed.