Converting char * to Uppercase in C
Asked Answered
A

5

18

I'm trying to convert a char * to uppercase in c, but the function toupper() doesn't work here.

I'm trying to get the name of the the value of temp, the name being anything before the colon, in this case it's "Test", and then I want to capitalize the name fully.

void func(char * temp) {
 // where temp is a char * containing the string "Test:Case1"
 char * name;

 name = strtok(temp,":");

 //convert it to uppercase

 name = toupper(name); //error here
 
}

I'm getting the error that the function toupper() expects an int, but receives a char *. Thing is, I have to use char *s, since that is what the function is taking in, (I can't really use char arrays here, can I?).

Any help would be greatly appreciated.

Assumption answered 3/2, 2016 at 16:5 Comment(0)
A
25

toupper() converts a single char.

Simply use a loop:

void func(char * temp) {
  char * name;
  name = strtok(temp,":");

  // Convert to upper case
  char *s = name;
  while (*s) {
    *s = toupper((unsigned char) *s);
    s++;
  }

}

Detail: The standard Library function toupper(int) is defined for all unsigned char and EOF. Since char may be signed, convert to unsigned char.

Some OS's support a function call that does this: upstr() and strupr()

Augment answered 3/2, 2016 at 16:8 Comment(1)
What systems normally support upstr? The Debian manpage for upstr says that it is from the "Alliance CAD System."Berdichev
F
10

For those of you who want to uppercase a string and store it in a variable (that was what I was looking for when I read these answers).

#include <stdio.h>  //<-- You need this to use printf.
#include <string.h>  //<-- You need this to use string and strlen() function.
#include <ctype.h>  //<-- You need this to use toupper() function.

int main(void)
{
    string s = "I want to cast this";  //<-- Or you can ask to the user for a string.

    unsigned long int s_len = strlen(s); //<-- getting the length of 's'.  

    //Defining an array of the same length as 's' to, temporarily, store the case change.
    char s_up[s_len]; 

    // Iterate over the source string (i.e. s) and cast the case changing.
    for (int a = 0; a < s_len; a++)
    {
        // Storing the change: Use the temp array while casting to uppercase.  
        s_up[a] = toupper(s[a]); 
    }

    // Assign the new array to your first variable name if you want to use the same as at the beginning
    s = s_up;

    printf("%s \n", s_up);  //<-- If you want to see the change made.
}

Note: If you want to lowercase a string instead, change toupper(s[a]) to tolower(s[a]).

Furst answered 1/3, 2019 at 10:15 Comment(3)
Curious, why use type unsigned long int s_len vs. unsigned s_len or even better size_t s_len? Seems odd to use unsigned long s_len and int a in a < s_len. I'd expect the variables to be the same type.Augment
Honestly I'm new to C and programming in general, so it is quite probable that the variable types are not the best ones.Furst
Computing the strlen before iterating through a string is very wasteful.Borzoi
M
6

toupper() works only on a single character. But there is strupr() which is what you want for a pointer to a string.

Mountford answered 3/2, 2016 at 16:8 Comment(2)
strupr is not standard. As far as I know it's only supported by Microsoft's library.Roderick
@interjay: it is supported also by Greenhills and Borland. But you are right, it is not in glibc.Mountford
B
5

How about this little function? It assumes ASCII represented chars and modifies string in place.

void to_upper(char* string)
{
    const char OFFSET = 'a' - 'A';
    while (*string)
    {
        *string = (*string >= 'a' && *string <= 'z') ? *string -= OFFSET : *string;
        string++;
    }
}
Buehler answered 7/8, 2020 at 17:29 Comment(1)
this gives bus error when compiled with -Wall -Wextra -WerrorPolaris
I
4

toupper() works on one element (int argument, value ranging the same as of unsigned char or EOF) at a time.

Prototype:

int toupper(int c);

You need to use a loop to supply one element at a time from your string.

Imbibition answered 3/2, 2016 at 16:8 Comment(1)
Maybe I will delete this answer, as it does not add value, but what's the reason for DV?Imbibition

© 2022 - 2024 — McMap. All rights reserved.