Question!

How do you convert lower case letters to upper case and vice versa?
in a string.

example;
Type a string: final ExaM is appRoaCHing
Reversed case: FINAL eXAm IS APPrOAchING
You can increase/decrease the character or you can use the toupper/tolower functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cctype>
#include <string>
using namespace std;

int main()
{
	
	string s = "final ExaM is appRoaCHing";

	for(auto &x: s)
	{ isupper(x) ? x=tolower(x) : x=toupper(x); }
	
	cout << s;
}
@anup30
What does ? mean? in line 12
@blitzz I think its another way of doing if statements, never done it that way myself though. I think it says

1
2
3
4
5
6
7
8
if (isupper(x))
{
    x = tolower(x);
}
else
{
   x=toupper(x);
}


Although Im not 100% sure
Last edited on
What does ? mean? in line 12

conditional operator: http://www.cplusplus.com/doc/tutorial/operators/
Topic archived. No new replies allowed.