String Library Functions

Hello all,
I am writing a program that takes a word, sends it to an array and uses that information to perform various library functions to manipulate it. The one I am currently on is I need to use the toupper library function to convert a word in an array to all capital letters. I am running into some problems, however as it is just not working.
I run the function I have using it through my head as if I were the compiler and everything seems alright, but for some reason it does not work. Further more, when I was experimenting with toupper earlier with single char letters, to get a feel for using it, it was returning an int, which I believe was the ascii dec version of what I was sending it, or the ascii dec version of what it was supposed to be converted to.
I would, of course like help with my initial problem, but if anyone could explain toupper better that would also be helpful, as the resources on this site are confusing me verses what my programming book tells me about the toupper function.

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
37
38
// Programmed by Kevin Seidel
// String Manipulation lab
// Part B manipulating words

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void upper(char []);

int main()
{
    int SIZE = 10;
    char wArray [SIZE];

    
    cout << "Please enter a single word" << endl;
    cin.getline(wArray, SIZE);
    cout << "The word you entered is: " << wArray << endl;
    cout << "\nThe length of your word is: " << strlen(wArray) <<" Letters" << endl;
    upper(wArray);
    
    system("pause");
    return 0;
}

void upper(char array [])
{
     int i = 0;
     while(array[i] != '\0')
     {
                    toupper(array[i]);
                    cout << array[i];
                    i++;
     }
     cout <<" is your word in all caps" << endl;
}
toupper() doesn't change its parameter, and it returns the uppercase letter.
It returns int for... historical reasons... and you may safely cast it back to char.

1
2
3
4
5
6
7
8
9
void upper(char array[])
{
    int i = 0;

    while (array[i] != '\0')
        cout << static_cast<char> (toupper(array[i++]));

    cout << " is your word in all caps" << endl;
}

You call the function toupper() but don't do anything with the result.
You should assign the result to something, like this:
array[i] = toupper(array[i]);

Other points. On line 14, SIZE should be a constant.
(the value is also a bit miserly, there are many words of more than 9 letters).
const int SIZE = 50;
Standard C++ requires that the array on line 15 is specified using a constant, hence the use of const on line 14.



As @Catfish2 pointed out toupper does not change its parameter. You shall use its return value to change the original character

1
2
3
4
5
6
7
8
9
void upper( char array [] )
{
     for ( int i = 0; array[i] != '\0'; i++ )
     {
                    array[i] = toupper( array[i] );
     }

     cout << array << " is your word in all caps" << endl;
}
Topic archived. No new replies allowed.