case change function is not working.

Im trying to reverse the case of each individual element in the array. The problem may be in the case changing function but I included main just in case.
I keep getting all upper case.

For example: If i inputed "Hello" I want to get back "hELLO" but instead i get HELLO

Heres my function that changes the elements.
1
2
3
4
5
6
7
8
9
10
11
void flip(char *array, int size)
{
   for (int i = 0; i<size; i++)
   {
       if (array[i] >= 'a' && array[i] <= 'z')
           array[i] = toupper(array[i]);

       else
           array[i] = tolower(array[i]);
   }
} 



Heres my main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Create sentence object
    string sentence;
    cout<<"Input a sentence: ";         // user inputted string
    getline(cin,sentence);
    const int LEN = sentence.length();// find length of user inputted string
    char line[LEN+1];                        // create array
    strcpy(line,sentence.c_str());       // copy string into array
    
    //Pointer
    char *strPtr;                              // create pointer to array
    strPtr = line;

// Flip String
    flip(strPtr,LEN);                         // call function
    cout<<"Your string flip: ";
    for (int i = 0; i < LEN; i++)       // display array
        cout << strPtr[i];
    cout <<'\n';

Last edited on
Topic archived. No new replies allowed.