Convert a String in C++ to Upper Case

Converting char * to Uppercase in C

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);


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()

Convert a String In C++ To Upper Case

Boost string algorithms:

#include <boost/algorithm/string.hpp>
#include <string>

std::string str = "Hello World";


std::string newstr = boost::to_upper_copy<std::string>("Hello World");

Use functions to convert string to upper and lowercase in C

You can just put your code to convert the string to upper and lower case in separate functions. like this.

void string_upper (char* str) {
for(int i=0; i<=strlen(str); i++) {
if(str[i]>=97 && str[i]<=122)

void string_lower(char* str) {

int main() {

char inputString[100], leftHalf[100], rightHalf[100];
int length, mid, i, k;


/* call function to convert string to upper-case*/
printf("String in Uppercase: %s\n",inputString);

/* call function to convert string to lower-case */
printf("String in Lowercase: %s\n",inputString);


return 0;

C string to uppercase in C and C++

The problem is that there is no sequence point in

while (*beg++ = toupper(*beg));

So we have undefined behavior. What the compiler is doing in this case is evaluating beg++ before toupper(*beg) In C where in C++ it is doing it the other way.

I am writing C function that convert lowercase char to upper case char with using ASCII but Output is not correct

This one will work:

#include <stdio.h>
#include <string.h>

void uppercase(char sent[]) {
for (int i = 0; i < (int)strlen(sent); i++) {
if (sent[i] >= 'a' && sent[i] <= 'z') {
sent[i] -= 32;

int main(int argc, char* argv[]) {
if (argc > 1){

return 0;

It compiles without any errors and warnings (using clang), even with options -pedantic -Wall -Wextra.

How to convert a string (char *) to upper or lower case in C

Your toUpper() implementation should work fine. However, you should pay attention to the warnings from your compiler (or turn up the warning levels if you didn't get any -- you should see something when you assigne a string literal to a char * like that). String literals are const, i.e. they cannot be modified. When you try to write to them, this will cause the segmentation fault you are seeing.

You need something like:

mentry->surname = malloc(4);
strcpy(mentry->surname, "bob");

or the more convenient, but not part of the C standard way:

mentry->surname = strdup("bob");

And of course, be sure to call free() later, either way.

Converting strings to uppercase C++

toupper(colour) returns an int you cannot add it (concatenate it) to a string.

For it to work you'll need to cast it to char

sTemp = sTemp + static_cast<char>(toupper(colour[iLoop]));

operator + concatenates two strings or a string and a char

Can't convert to Lower/uppercase a char* pointer using a loop in C without toupper

You may not change string literals. Any attempt to change a string literal results in undefined behavior.

From the C Standard (6.4.5 String literals)

7 It is unspecified whether these arrays are distinct provided their
elements have the appropriate values. If the program attempts to
modify such an array, the behavior is undefined.

So instead of this call

toLowerCase("HELLO WORLD");

use at least

char s[] = "HELLO WORLD";

toLowerCase( s );

Pay attention to that it will be much better when the function will return pointer to the modified string and will not output the string. It is the caller of the function that will decide whether to output the modified string.

So define the function like

char * toLowerCase( char *string )
// ...
return string;

Converting strings to uppercase to compare with user input in C

use the following function, which is included in strings.h

int strcasecmp(const char *s1, const char *s2);

in your case change if statement

if(strcmp(uppberbname,uppername) == 0)


if(strcasecmp(bname,name) == 0)

and delete

char uppername[40] = toupper(name[15]);
char upperbname[40] = toupper(bname[15]);

Related Topics

Leave a reply