Changing lowercase to upper case letter and Vice versa

Pages: 12
Hi guys I need help with how to create a function whose input is a string. The function has create a new string from old one by changing all lowercase letter to uppercase letters and viceversa. It should leave all other characters unchanged thus the biggest problem, i couldn't figure how to do.

I have a program that can change all letter to uppercase but i don't know how to do the rest of the problem. Any help please!!!

#include <iostream>
#include <string>
#include<cstdlib>
#include <ctype.h>
#include <string.h>
using namespace std;

int main()
{
cout << "string: ";
char s[256];
cin.get(s, 256);

for(int i = 0; i < strlen(s); i++)
s[i] = toupper(s[i]);
cout <<endl << s << endl;

system("Pause");
return 0;
}
Just check if the ascii character of the letter you're looking at is a lowercase letter before modifying it. So check if it's in between 97 to 123 (check those numbers, they might be wrong).
You don't need to (and shouldn't) use the numbers.
1
2
3
4
5
6
7
8
if(s[i] >= 'a' && s[i] <= 'z')
{
    //s[i] is lowercase
}
else if(s[i] >= 'A' && s[i] <= 'Z')
{
    //s[i] is captial
}
This relies on a-z and A-Z being consecutive in the character set.
Last edited on
L B Thank you, but i don't know
how do we declare the function s[i]?

while compiling , i get this error: storage size of 's' isn't known ;(

#include<iostream>
#include <cstdlib>
using namespace std;

int main()
{
char s[];
char i;
cout << "string: ";
if(s[i] >= 'a' && s[i] <= 'z')
{
cout << "s[i] is lowercase" << endl;
}
else if(s[i] >= 'A' && s[i] <= 'Z')
{
cout << "s[i] is uppercase" << endl;
}
system("Pause");
return 0;
}

I wrote that code based on your original program, the one you posted in your first post. In specific, you had these lines which were fine:
9
10
11
    cout << "string: ";
    char s[256];
    cin.get(s, 256);
Last edited on
:(
The program doesn't convert lowercase to uppercase or anyhow??
it compiles and say press any key to enter

#include<iostream>
#include <cstdlib>
using namespace std;

int main()
{
cout << "string: ";
char i;
char s[i];
cin.get(s, i);

if(s[i] >= 'a' && s[i] <= 'z')
{
cout << "s[i] is lowercase" << endl;
}
else if(s[i] >= 'A' && s[i] <= 'Z')
{
cout << "s[i] is uppercase" << endl;
}
system("Pause");
return 0;
}
Why would you expect it to convert from lowercase to uppercase and vice-versa? All you're doing is print out whether each character is capital or lowercase, which is code I gave you as a head start and to push you in the right direction.

To convert an uppercase letter to a lowercase letter, subtract 'A' and add 'a'.
To convert a lowercase letter to an uppercase letter, subtract 'a' and add 'A'.
L B is this what u mean?

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

#include<iostream>
 #include <cstdlib>
 using namespace std;
 
int main()
 {
 cout << "string: ";
 char i;
 char s[i];
 cin.get(s, i);

// for ( i = A-a)
// { cout << " convert an uppercase letter to lowercase." << endl;}

 if(s[i] >= 'a' && s[i] <= 'z')
 {
 cout << "s[i] is lowercase" << endl;
 }
 else if(s[i] >= 'A' && s[i] <= 'Z')
 {
 cout << "s[i] is uppercase" << endl;
 }
 system("Pause");
 return 0;
 }

anyone know how to use <toupper> and <tolower> functions for changing lowercase to uppercase and viceversa

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<cctype>
#include<iostream>
#include <cstdlib>
#include<string>
using namespace std;

int main()
{
    cout << "string: ";
    char i;
    char s[i];
    cin.get(s, i);
    
    if(s[i] >= 'a' && s[i] <= 'z')
        {
            cout << "s[i] is lowercase" << endl;
            cout << toupper(s[i]) << " s[i] " << endl;
        }
    else if(s[i] >= 'A' && s[i] <= 'Z')
        {
            cout << "s[i] is uppercase" << endl;
            cout << tolower(s[i]) << "s[i]" << endl;
        }
    system("Pause");
    return 0;
}


closed account (3qX21hU5)
I dont think you get what LB is saying... The code he posted you should implement into your original code. It wont convert Upper case to lower case and vice versa, you need to figure out how to change it to do that. I would recommend using a loop to run through each letter and determine if it is lowercase or upcase then change it accordingly. Remember the cplusplus.com/reference has alot of great info that can help you
Last edited on
What is this supposed to do?
1
2
    char i;
    char s[i];

It's not valid C++ code and doesn't even compile.
Just as importantly, if it did compile, char i; is uninitialised, it contains garbage. The next line attempts to allocate an array containing {garbage} number of elements.

It's not a very good starting point for the code which follows it.

Sorry if this sounds overly critical, my intention is help to improve the code, even if it sounds a bit abrupt.
Last edited on
Thanks Zereo and Chervil
thus why am asking for help.
I mean i would appreciate if one could code the program the way is supposed to be and help me fix what i wrote so that my program could compile.
& Chervil I don't mind the comments thus how i do learn.
Thanks guys.
Ya, like chervil said, what it seems like you are trying to do is use a char array to read the input. Also, you included the string header but aren't using any strings. If you want to the char array I'd use a seperate variable then i to initialize it, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <cctype>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    cout << "string: ";
    int size = 100;
    char array[size];
    cin.get(array, size);


Now the part where you convert from upper to lower or vice versa is an if loop that will only execute once. Here it'd be better to use a simple for loop. Also, the cctype library has 2 functions that help here called isupper and islower. They both test an alpha character and return a type bool if the char is upper (or lower, respectively) so they can easily be used as a test expression as such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 for (int i = 0; i < size; i++)
{
    if (isupper(array[i]))
    {
          cout << "array" << i << " is uppercase" << endl;
          // not sure why you want to convert it to lower then and output but here's that
          cout << tolower(array[i]) << "array" << i << endl;
    }

    else if (islower(array[i]))
    {
          cout << "array" << i << " is lowercase" << endl;
          cout << toupper(array[i]) << "array" << i << endl;
    }



Also, you could add a bit of code to check if the char is an alphabetic char to begin with using isalpha but this here should get you started on the right track for that.

In fact, now that I think about it, you could use a while loop instead of the for to do the alpha check for you. Something like:

1
2
3
4
5
6
7
int i = 0;
while (isalpha(array[i]))
{
    // if/else stuff goes here

i++ //make sure to increment i that it steps through the array char by char until it hits the null char, or a number... of
}


The conditional expressions LB showed work much the same way as toupper and tolower so you can use either, it's just that the two I showed are actual function that you must pass the character to be tested to and that return type bool. As such, they may be a bit beyond what you are learning atm.
Last edited on
Thank u Raezzor
but the program still doesn't run :(

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
#include<cctype>
#include<iostream>
#include <cstdlib>
#include<string>
using namespace std;

int main()
{
    cout << "string: ";
    int size = 100;
    int i;
    char array[size];
    cin.get(array, size)
    
    for (int i = 0; i < size; i++)
  {
    if (isupper(array[i])
    {
          cout << "array" << i " is uppercase" << endl;
          // not sure why you want to convert it to lower then and output but here's that
          cout << tolower(array[i]) << "array" << i << endl;
    }

    else if (islower(array[i])
    {
          cout << "array" << i " is lowercase" << endl;
          cout << toupper(array[i]) << "array" << i << endl;
    }
 } 
    system("Pause");
    return 0;
      

}

Hmm ya, the program has a few errors, I was just throwing out some ideas for you. Here's what I can see off the bat without trying to actually run it: For one thing, lines 17 and 24 should have 2 close parentheses at the end of the line instead of 1. Also lines 19 and 26 should have an additional << operater between the i variables and the "is etc.etc." That's easy debugging though. I'll edit my post above with the code in it to reflect these. Also, you can get rid of line 11 since you are declaring it in the for loop.

Also, now that I look at it a bit more, there's nothing stopping this program from running past the end of the word straight through to the end of the array. You'll have to add a loop or something to check if the char being read is a null char (\0) and if so break out of the for loop.
Raezzor wrote:
10
11
    int size = 100;
    char array[size];
How could you do such a thing when it was just discussed that this is not valid C++?
Why not use std::string, utilizing the operator [] to sequentially access members of the string?
Because a lot of C++ teachers are mean/evil/deprecated and make their students use character arrays on the stack to store strings and take off points for not following directions that say to use character arrays instead of std::string.
Let's look at three variations of this tiny bit of code.
1
2
3
4
5
    char i;
    char s[i];

    int size = 100;
    char array[size];

The problem with lines 1 and 2 is that int i is not uninitialised.
Lines 4 and 5 fix this. Some compilers will accept this, but it isn't standard, as size is a variable. But if it compiles without error then it should be fine.

A third version:
    const int size = 100;
    char array[size];

Here, the size is made a constant and the code should compile as C++ without errors.

Actually I can see good reasons for using character arrays during a learning exercise. The important thing there is to have full control and understanding of what is happening.

The C++ std::string has advantages, but it may be treated as a 'black box' whose inner workings are of no concern. That means it may be less useful for certain parts of studying as the mysterious inner workings may simply remain mysterious.

I certainly don't think it's mean or bad for a teacher to ask for things to be done the long or old-fashioned way. For example calculating the sum of a series rather than using built-in functions for sin, cosine and so on is a perfectly valid exercise. The point here is not that you will actually have to use this approach in real-life projects, but rather that you will have a general idea of how the computer works internally instead of it just being a box which does something when you press a button.
Last edited on
LB, I thought you were saying that using type char to declare the size of an array wasn't valid, which I can understand. I see what you mean though that it's proper to use const rather then a variable.
Pages: 12