Sort array uppercase!

Hi, I need to take the array of;

string name[4] = {"Bill", "Adam", "Mary", "David };

and create a function which sorts the names and puts them all in uppercase.

How do I do this if toupper is only for chars? How would I have the string array be manipulated to take each single char?

How do I do this if toupper is only for chars?
Apply toupper to each char in string:
1
2
3
for(std::size_t i = 0; i < 4; ++i) {
    std::transform(name[i].begin(), name[i].end(), name[i].begin(), ::toupper);
}

and create a function which sorts the names and puts them all in uppercase.
std::sort(name, name + 4);
What is size_t? we did not learn this in class I believe.

What is std:: transform? what is the member .begin, .end?
What is size_t?
Unsigned integer type which is large enough to index any array. Technically I should have used std::string::size_type, but I was lazy. You can use int if you want.
what is the member .begin, .end?
They returns iterators to the first element and past the end respectively. Iterators are integral part og C++ and standard library. They (and begin/... member function) exis in all STL containers.

What is std:: transform?
It is the standard algorithm which applies function (passed last) to each element in range (first two parameters) and stores result in range starting from passed iterator (Third parameter). If first and second parameter are same it saves result of application of function to the element in same element. Like c = toupper(c)
http://en.cppreference.com/w/cpp/algorithm/transform
What is ...

The "search and use" of reference material is an important skill to learn.

http://www.cplusplus.com/reference/
http://en.cppreference.com/w/cpp
if you want to do without libraries...

string name[4] = {"Bill", "Adam", "Mary", "David" };

for(char &ch: name[0]) //Bill
{
if (ch>='a' && c<='z')
ch = c- ('a' - 'A');
}
cout << name[0]; //BILL

using outer loop you can convert all names.

then sort the array alike integer array.

Last edited on
if (c>=97 && c<=122)
c = c- ('a' - 'A');
Technically non-portable. Target enviroment does not required to use ASCII encoding.
corrected. gcc may show different result in windows and linux?
gcc may show different result in windows and linux?
I doubt it.
However whole line c = c- ('a' - 'A'); is unportable. You should not make assumptions on how characters are stored in codepage. Use library functions:
c = toupper(c). It is faster than calculation. It will be correctly imlemented on target enviroment. It is self-descriptive.
yes, libraries make coding easier. but at beginner level one should try non-library versions to explore the language. such as if the OP sorts using algorithm library, the professor may doubt that as copy-code.
do you know any recognized C++ environment, where int('a') != 97
Yes, we support several programs for industrial controllers which uses nonstandard codepage. Namely letters are grouped as: "AaBbCc...ZzАаБбВв...Яя0123456789". Additionally I can simply change codepage used in linux terminal and se incorrect output.

Additionally your example is not working even in normal Windows enviroment for those who uses not only English.

I am all for learning and reimplementing standard algorithms when it is exactly what is needed: you need to implement sorting — OK; you need sorted data — use standard library.

One of the worst programming sins is reinventing the wheel. So it is better to know what language has to offer you then to learn specifics. Like Top-down learning.
Topic archived. No new replies allowed.