Converting a string to lower case to upper case and vise versa.

If anyone could help out that would be great

I have a hw assignment that wants me to read from a inputfile a list of metals. The hw assignment wants me to ask the user for a metal. However if the he metal entererd is all lower, all upper, or a combination, the hw assignment wants me to find that metal in the inputfile which is for example just 'Copper'. How can I convert what the user enters to just one meaning.
C and C++ have built-in functions to convert ints (or chars, it won't complain) to lower or upper case, called tolower() and toupper(). Here's an example:
1
2
	std::cout << tolower('H');		//104- the ASCII value of 'h'
	std::cout << (char)tolower('H');	//'h' 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <ctype>
#include <iostream>

void main(){
	char* myOutput = "HELLO THERE";
//loop through each character and make it lower-case. stop when you hit '\0'.
	for(int i = 0; myOutput[i] != '\0'; i++){
		myOutput[i] = tolower(myOutput[i]);
	}
//Make the first letter capital.
	myOutput[0] = toupper(myOutput[0]);

	std::cout << myOutput;				"Hello there"
}
Last edited on
You could also use the STL transform() algorithm. More details here:
http://www.devx.com/getHelpOn/Article/9702/1954
Topic archived. No new replies allowed.