Upper Case to Lower case Letters Using Strings

Hey everyone! This is my first time on this site and I have seeked out aid in my CS 171 class as I am very confused and can't seem to find any help in the textbook, online, or class notes for this problem.

The idea is to use only use strings, I will post what I got below directly from the page, but I can't see to find the one bit of code that would convert the ch into an upper case.

"Because the purpose of this assignment is to gain experience with the specified objects and methods, you are to do each of these problems as described and not search for alternative approaches (no if statements, character arrays, apstrings, or atoi).

The char and int types allow mixed-mode arithmetic and assignment. For instance,the following code segment produces the letter 'e' as output:
char ch = 'a';
ch = ch + 4 ;
cout << ch << endl ;
Using such arithmetic, write a segment of C++ code that will convert a lower-case letter to upper case. That is, if ch has the value 'f', it will be changed to 'F'. You may assume the value of ch is a lower-case letter."

Each character has a 1 byte value associated with it which is defined by ASCII. For example, 'a' is 97. C++ allows you to refer to the character by its integer value. For example, executing the following would print a 'b'.

std::cout << (char)('a' + 1);

The exact encoding of ASCII characters can be easily found online.
http://www.asciitable.com/

Noting this, how would you convert an uppercase 'A' to a lower case 'a'?
Last edited on
ASCII:
a = 97
A = 65
a - A = 32

There, lowerCase - 32 = UpperCase UpperCase + 32 = lowerCase
Last edited on
Thank you so much to the both of you for submitting so quickly! I am going back to this problem right now to try and figure it out, I'll be sure to post if I got it soon for you all to know how I did.
Hey so I am stuck again sadly.
I am not sure if I am going overboard here but from the phrasing of the question I would want to make it so any lower case value a user submitted would be able to be converted to it's uppercase version. My problem comes when I run the said code.

int main()
{
string user;

cout << "Please enter a lower case letter." << endl;
getline(cin,user);

char ch = 'user';
ch = ch - 32 ;
cout << ch << endl ;


return 0;
}

in which I am always supplied an R. I am assuming that this is because it is grabbing simply the little r at the end of user always. Any idea on how to get it work with a user submitted value?

P.S. In your example denormal i would convert an Uppercase A to lower case a by using the code.

int main()
{

char ch = A
ch = ch + 32 ;
cout << ch << endl ;


return 0;
}

Simple revering the way I did before would result in the end result of an Uppercase letter.
You don't have to know the difference in character encoding between 'A' and 'a'. If you start with a lower case letter then letter-'a' is a number between 0 and 25. If you then add that to 'A' you get a letter between 'A' and 'Z';
1
2
char lower = 'f';
char upper = lower - 'a' + 'A';


Note that this ONLY works if you start with a letter to begin with. If you start with, say '#' then you'll end up with who knows what.
Thx for the reply dhayden but what about if the user wants to upload a number? I was getting an error when I tried to work with this about converting strings to char and I know I need to use strings to some degree with this. Maybe I am missing what you are saying and you answered this >.< I am very confused with C++ at the moment especially where strings are concerned.
Using such arithmetic, write a segment of C++ code that will convert a lower-case letter to upper case. That is, if ch has the value 'f', it will be changed to 'F'. You may assume the value of ch is a lower-case letter."

Line 2 of my example does exactly this.

but what about if the user wants to upload a number? I was getting an error when I tried to work with this about converting strings to char

Is this required? Your first post says
You may assume the value of ch is a lower-case letter
. In other words, it won't be a number.

If you need to convert the lower case letters in a string to upper case then check each character in the string. If it's a lower case letter then use the formula above to replace it with it's upper case equivalent.
Hi,

Looking at the part of the assignment and your code there is a fair bit of confusion going on.

First - where did you get the idea to use strings - from the assignment text, it would seem to me to specifically NOT to use strings only char. std::string is a class which is part of the Standard Template Library (STL) and it has a whole lot of built in functionality.

Notice I used code tags - you should always do this too when posting here. Use the <> button on the format menu.

...... and can't seem to find any help in the textbook, online, or class notes for this problem.


The answer for how to do it right there in the assignment.

I put some comments in your code

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
#include <iostream>

// don't have using namespace std - put std:: before each std thing

// this is a simple example that relies on the order of the ascii table

int main()
{
string user;  // std::string not a suitable type
char InputChar = 'a'; // ALWAYS intialise your variables to something
char OutputChar ='A';

std::cout << "Please enter a lower case letter.\n"; 
getline(cin,user); // std::getline is handy when there are spaces in the input
                                   // not necessary for a single char
std::cin >> InputChar ;

char ch = 'user';  //type mismatch here and a char only has 1 character
                                   // if you want to assign a value from a variable name then don't use quotes
ch = ch - 32 ;  // read what dhayden said about this
                      

// you can fix up this bit


std::cout << OutputChar << std::endl ; 


return 0;
}


I guess the main thing is to realise what the different types are and how to use them. For example a char holds only 1 character, and has single quotes.

Topic archived. No new replies allowed.