Changing upper to lower case and vice versa for a string?

Mar 22, 2014 at 6:06am
ex: "My Name Is John" should be "mY nAME iS jOHN"

i have a really long code that has other stuff but i'll put it anyway
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  #include <iostream>
#include <cctype>
#include <string>
using namespace std;

int main ()
{
	string xstring; 
	char choice;
	char Q, q;
	


	cout << "Enter a string: ";
	getline(cin, xstring);
	
	do
	{
		cout << "USE THIS MENU TO MANIPULATE YOUR STRING\n";
		cout << "----------------------------------------\n";
		cout << "1) Inverse the string\n";
		cout <<	"2) Reverse the string\n";
		cout <<	"3) To Uppercase\n";
		cout <<	"4) Jumble String\n";
		cout <<	"5) Count Number of Words\n";
		cout <<	"6) Count Constants\n";
		cout <<	"7) Enter a Different String\n";
		cout <<	"8) Print The String\n";
		cout <<	"Q) Quit\n";
		cout << "Enter your choice: ";

		cin >> choice;
		cout << endl;

		if (choice == '1')
		{

			
		}

		else if (choice == '2')
		{
			xstring = string( xstring.rbegin(), xstring.rend() );
			//cout << xstring;
		}

		else if (choice == '3')
		{
			for(int i = 0; i < xstring.length(); i++)
			{
				xstring[i] = toupper(xstring[i]);
				//cout << xstring[i];
				
				
			}

		}

		else if (choice == '4')
		{
		}

		else if (choice == '5')
		{
		}

		else if (choice == '6')
		{
		}

		else if (choice == '7')
		{
		}

		else if (choice = '8')
		{
		}

		//else
			//cout << "Error! Please type in a valid choice\n";
		

	}
	
	while (choice != 'Q' && choice != 'q');
	{
		
	}
	
	




	system("PAUSE");
	return 0;

}
Mar 22, 2014 at 6:22am
You can use the islower/upper and tolower/upper functions if you want.
http://www.cplusplus.com/reference/cctype/

Or you could iterate through the string and check manually with something like:

1
2
3
4
5
6
7
8
if(str[i] >= 'a' && str[i] <= 'z') //lowercase found
{
    str[i] -= ' '; //'a' == 97, 'A' == 65, ' ' == 32, if you prefer you can subtract 32
}
else if(str[i] >= 'A' && str[i] <= 'Z') //uppercase
{
    str[i] += ' '; //as mentioned earlier
}


Looking at your code earlier it looks like you can use the to/is(upper/lower) functions so use those instead of what I mentioned.

To get your code working you should check if the character is tolower(or upper) then either make it upper or lower depending so instead of: xstring[i] = toupper(xstring[i]); try something like:
1
2
3
4
5
6
7
8
if(islower(xstring[i]))
{
    xstring[i] = toupper(xstring[i]);
}
else
{
    xstring[i] = tolower(xstring[i]);
}


[edit]
You seem to know what iterators are based on your reverse function why not iterate over the string instead of using a 0->n loop?

1
2
3
4
5
6
7
8
9
for(const auto &it : str) //you can use char instead of auto I suppose
{
    // 'it' is the character
}

for(auto it = str.begin(); it != str.end(); ++it)
{
    // '*it" is the character
}
[/code]
Last edited on Mar 22, 2014 at 6:29am
Mar 22, 2014 at 7:37am
giblit wrote:
1
2
3
4
5
6
7
8
if(str[i] >= 'a' && str[i] <= 'z') //lowercase found
{
    str[i] -= ' '; //'a' == 97, 'A' == 65, ' ' == 32, if you prefer you can subtract 32
}
else if(str[i] >= 'A' && str[i] <= 'Z') //uppercase
{
    str[i] += ' '; //as mentioned earlier
}


That is a bit obfuscated. The space character has nothing to do with the conversion from or to different cases, logically speaking. It just happens to have the same value in ascii as the distance from 'a' to 'A'.

1
2
3
4
if(str[i] >= 'a' && str[i] <= 'z') //lowercase found
    str[i] = str[i] - 'a' + 'A';
else if(str[i] >= 'A' && str[i] <= 'Z') //uppercase
    str[i]  = str[i] - 'A' + 'a';
Mar 23, 2014 at 12:53am
i don't know what iterators are at all. I got that from the internet....
Mar 23, 2014 at 12:57am
I wouldn't suggest using it if you copy/pasted it..
Mar 23, 2014 at 2:00am
Thank you very much giblit, I really appreciate your help so much
Topic archived. No new replies allowed.