Changing lowercase to upper case letter and Vice versa

Pages: 12
Raezzor thanks, i MADE edit version :)

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
#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; 
          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;
}
This program is still invalid and doesn't compile. Declaring an array with a non-constant size, re-declaring i in the same scope, using system()...
Don't use system("Pause"). Read the article on why system() is evil for more information. Instead use:
1
2
3
4
5
6
7
8
void PressEnterToContinue()
{
      int c;
      cout << "Press ENTER to continue."<<endl;
      fflush(stdout);
      do c = getchar();
      while ((c != '\n') && (c != EOF))
} //end PressEnterToContinue 

I had your exact same problem. I tried using toupper() and tolower(), but they only PRINT the uppercase and lowercase (for me, this is useless, because I am trying to convert it to ALL ONE CASE to pass it as variable value).
Look more into the advice you were given and the types of functions (and data) you are trying to use. Then you will understand L B's query. Right now, I am fighting the urge to laugh because I was once in your shoes (and still kinda am) lol. I think this will persist as long as we are students, which, given that this is computer SCIENCE, will be as long as we live, until we decide we no longer want to learn. Science is ALL ABOUT making mistakes, observations on those mistakes, and working to correct them.

Oh, and you REALLY need help on data types. s[i] is an element of array s. (Or, since C-style strings are just arrays of characters, s[i] == ith character of string s.) Look into data types (don't ignore them!) and you will get this! Take L B's advice on this one...
Last edited on
Oh, and Gulshan Singh, you could use the numbers, but you would have to cast them to char. I had a situation in MY project where the user was supposed to type in the full directory, and the program was supposed to open a file in it. C++ would treat the '\' as part of a special character, so I couldn't use it directly and I had to refer to it via type-casting : (char)92.
closed account (3qX21hU5)
Like Hotice and LB are saying you need to look at the advice and research it. Learn what everything is that you dont understand. You cant just copy and paste the snippets of code that we give you and expect them to work. You need to learn from them they are just general ideas for you to build on and then once you get the idea of what they do implement them into your program.

From what I seen you basically just copied and pasted everyone else code together, which is not really learning the material.

I would reccomend you reread these 3 subject in your textbook or search on here for some tutorials on them.

First is Arrays, I don't think you totally grasp the idea yet but you do have some idea of what they do so maybe just skim over it a little and see if you missed anything, like you can only use constant expressions or const variables for the size of a array.

Second look into if and if else. This is how you are going to figure out how to determine if the letter is uppercase or lowercase and then do something to change it.

Third look into data types once again, I have a feeling you got a good grasp on it but are missing a few key points on it so it might help to look into again.

There is plenty more to learn so getting a good grounding on the base elements of C++ will help you in the long run. Keep up the good work and dont worry its always hard beginning something but it get better so just stick with it. And sorry if I sounded a tad bit critical.
Topic archived. No new replies allowed.
Pages: 12