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.