Advice on creating a toupper function for c string

For a program I am writing I am restricted from using the toupper from #include<string> and am required to write my own toupper function as void toUpper(char* str), with the use of a c string such as char[];. How would I go about starting it?

Last edited on
I'm assuming you are also prohibited from using the <cctype> toupper function?

There are few questions you will have to address to then determine the cases the function will be dealing with.

Are you taking in a single character?
Are you taking in a string of multiple words?
Are you wanting to capitalize all characters of an entire char*?
Are you wanting to capitalize only specific characters in a char* of unspecified length?

After that, consider what you are doing. You are retrieving the ASCII value of a given lower-case character and replacing it with the ASCII value of it in a upper case.
Last edited on
I would like to capitalize a string of a word and multiple words like, char test[] = "capitalize me";

and the function layout has to be like

void toUpper(char* str)
{

I was wondering if if statements would be a good of use?

}
Last edited on
So you are capitalizing the entire string?
All you have to do is determine the length of the string, and for each letter, add 32 to that index (adding 32 to a lower case letter replaces its value with its capitalized value).

if you are using string, you can do this with the length() function from the string class. if you are using a character array input, you can use strlen (from cstring)

Basic algorithm:
take in data from the user (in either a string variable, or a char[] variable).
Pass string or char[] to void toUpper(char*str).
Get length of string.
Run a loop for the length of the string.
Use an if check to confine the operation to lower case letters.
If the letter is lowercase, replacing it with its letter +32 (its ASCII capitalized).
Last edited on
So something like this

1
2
3
4
5
6
7
8

void(char* test)
{
for(int i = 0; i < strlen(test); i++)
{
     test[i+32];
}
}
Last edited on
The C++ library <string> doesn't have a toupper() function, that is part of the C library <cctype>. <string> might include <cctype> in some implementations, but it isn't a requirement.

Working on an entire C string instead of individual characters makes for a more complex algorithm.

If that is your function prototype you are required to use, then a custom toupper() function that works on an entire C string instead of a single char might consist of the following steps:

1. determine the length of the C string by finding the terminating null ('\0'). If that fails, return an error the string is not valid. Unfortunately there is no method to indicate your C string has no null terminator with your function prototype. So you have to assume your C string is a proper C string.

2. loop through your valid C string. With each character determine if it is an alphabetic character that can be either upper or lower case.

2a. If not an alphabetic character (number or punctuation) go onto the next character in the string (hint, continue statement).
http://www.cplusplus.com/doc/tutorial/control/

2b. If it is an alphabetic character check if it is already upper-case. If so, next character. If not subtract 48 (x30 hex) from it to convert it from lower-case to upper. See ASCII code table.
http://www.cplusplus.com/doc/ascii/
If we start just a bit simpler,
do you know how to change a single character into its uppercase form?
e.g. a function that looks like: char to_uppercase(char input) { ... }
Yeah, you do it like this


1
2
3
4
5
6

                for(int i = 0; i < strlen(str); i++) 
                {
                    cout << char(toupper(str[i]));
                }
Last edited on
Right, good. All you need to do, instead of printing the character, is to set str[i] to the output of toupper.
Last edited on
Gotcha! Thanks for the help
Topic archived. No new replies allowed.