String to Char

Hello,
I am new to programing and I have this home work assignment that I need help solving. :)

What I need my program to do is convert user input into all uppercase and all lower case --Ex: *I type in this phrase*= toupper(I TYPE IN THIS PHRASE)
I realize that a simple solution is to convert my string into a char then applying the toupper/tolower conversions. The problem is I am having trouble finding resources. Will anyone be so kind as to point me in the right direction?


Here is my program.
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
#include <string>
#include<cctype>
#include <iostream> 
using namespace std;



 
int main()
{
string user_input;
char convert;
cout<< "Hello, please enter a string(s) and I will tell you the following:\n";
cout<< "The phrase backwards\n";
cout<< "The phrase in caps and lower case\n";
cout<< "And the number of constinants and vowls!\n\n";

getline(cin, user_input);
cout<< "\n\n";
cout<<(user_input);

user_input=string(user_input.rbegin(),user_input.rend());
cout<< "\n\n";
cout<< "The string entered in reverse is\n\n";
cout<< "\n"<<user_input<<"\n";

cout<< "TEXT DISPLAYED IN ALL CAPPS:\n";
//user_input = all caps as to not confuse the program

convert = user_input;
user_input= char (toupper(user_input));
cout<<(user_input);
system("PAUSE");
return 0;
}




closed account (o1vk4iN6)
You can use transform:

1
2
3
string str;

std::transform(str.begin(), str.end(), str.begin(), toupper);
Thank You for your reply!

However, I don't know if I am using your code correctly. Here is my revised code (also, do i need to include
a new "#include<>" for this code to work?)

1
2
3
4
5
string user_input;

std::transform(user_input.begin(), user_input.end(),user_input.begin(), toupper);

cout<<"The string all caps is="<<user_input<<"\n";

1 error C2086: 'std::string user_input' : redefinition c:\users\alan\documents\visual studio 2008\projects\project1\ff\ff\ff.cpp 29
Error 2 error C2039: 'transform' : is not a member of 'std' c:\users\alan\documents\visual studio 2008\projects\project1\ff\ff\ff.cpp 31
Error 3 error C3861: 'transform': identifier not found c:\users\alan\documents\visual studio 2008\projects\project1\ff\ff\ff.cpp 31


Error
Last edited on
transform is in algorithm.

#include <algorithm>
OMG! Thank You so much! I don't know how algorithms work. I don't want to use something I don't understand so is there a website where I can read on algorithms and how to use them? In other words is there a website anyone recommends that will explain how Xerzi's code is derived? Free lattes for everyone :)

Thank You Alrededor :)
Transform is explained pretty well here: http://www.cplusplus.com/reference/algorithm/transform/
Topic archived. No new replies allowed.