143

How can I convert a mixed case string to a lowercase string in C?

5
  • 2
    Are you just dealing with ASCII with letters a-z only?
    – Mark Byers
    Apr 18, 2010 at 9:52
  • 1
    ascii. how would i take that into account? would the example below still work? what happens if my char is a '#' and tolower() gets called on it?
    – Tony Stark
    Apr 18, 2010 at 10:10
  • 1
    That will work. I was more thinking if your string contains things like é or Ü.
    – Mark Byers
    Apr 18, 2010 at 10:46
  • 4
    Why not just use "strlwr"? strlwr((char*)str); It just goes through the string and converts it itself.
    – Larry
    Mar 1, 2018 at 23:49
  • 1
    @Larry It's non-standard.
    – mid
    May 28, 2018 at 20:45

5 Answers 5

209

It's in the standard library, and that's the most straight forward way I can see to implement such a function. So yes, just loop through the string and convert each character to lowercase.

Something trivial like this:

#include <ctype.h>

for(int i = 0; str[i]; i++){
  str[i] = tolower(str[i]);
}

or if you prefer one liners, then you can use this one by J.F. Sebastian:

for ( ; *p; ++p) *p = tolower(*p);
10
  • 43
    for ( ; *p; ++p) *p = tolower(*p); seems more idiomatic.
    – jfs
    Apr 18, 2010 at 9:58
  • 16
    @J.F. there you go. Depends on if they want the code to look scary or nice :) (very readable one liner, but it does look scary)
    – Earlz
    Apr 18, 2010 at 10:05
  • 1
    this gives me a segfault if str is a char *, but not if str is a char array. Got any explanation for that? Nov 22, 2016 at 22:07
  • 5
    I believe the one liner will cause you to lose your pointer to the string.
    – Ace.C
    Sep 8, 2017 at 19:55
  • 2
    I believe that one liner will have untold ramifications. Mar 25, 2018 at 1:12
9

to convert to lower case is equivalent to rise bit 0x60 if you restrict yourself to ASCII:

for(char *p = pstr; *p; ++p)
    *p = *p > 0x40 && *p < 0x5b ? *p | 0x60 : *p;
8
  • 7
    To make it slightly more readable you could do for(char *p = pstr;*p;++p) *p=*p>='A'&&*p<='Z'?*p|0x60:*p; Apr 18, 2010 at 10:54
  • 8
    This version is actually slower than glibc's tolower(). 55.2 vs. 44.15 on my machine.
    – jfs
    Apr 18, 2010 at 18:10
  • i can't imagine that: tolower() deals with chars; only if it's macro Apr 18, 2010 at 18:37
  • 1
    @oraz: tolower() has int (*)(int) signature. Here's the code used for performance measurements gist.github.com/370497
    – jfs
    Apr 18, 2010 at 19:32
  • 1
    The fastest version uses a lookup table instead of branches.
    – Joe
    May 8, 2020 at 23:24
7

Looping the pointer to gain better performance:

#include <ctype.h>

char* toLower(char* s) {
  for(char *p=s; *p; p++) *p=tolower(*p);
  return s;
}
char* toUpper(char* s) {
  for(char *p=s; *p; p++) *p=toupper(*p);
  return s;
}
4
  • Well if you're going the one-liner way, then s is a local variable in your function, you can directly use it instead of declaring p.`
    – NewbiZ
    Mar 8, 2022 at 2:32
  • @NewbiZ, indeed: // convert string to lowercase, in place: char* toLower(char* p) { for( ; *p; p++) *p=tolower(*p); return p; } Jun 26 at 4:18
  • 1
    @NewbiZ, not given this signature (which returns pointer to the start of string). A copy needs to be made somewhere! Oct 13 at 15:06
  • @TobySpeight, oh right indeed
    – NewbiZ
    Oct 15 at 8:38
1

If we're going to be as sloppy as to use tolower(), do this:

char blah[] = "blah blah Blah BLAH blAH\0";
int i = 0;
while( blah[i] |=' ', blah[++i] ) {}

But, well, it kinda explodes if you feed it some symbols/numerals, and in general it's evil. Good interview question, though.

3
  • 7
    Yeah, this will fold/spindle/mutilate a variety of symbols (in ASCII, any symbol, control character, or numeral with bit 5 clear will become the same character code with bit 5 set, etc) so really, seriously, don't use it.
    – Ken S
    May 22, 2013 at 21:26
  • 1
    This post is discussed on meta. Sep 2, 2014 at 8:31
  • Can you elaborate more? When I read about tolower(), they all mention that they only work on characters that have a lowercase character defined for them. From opengroup.org: "If the argument of tolower() represents an uppercase letter, and there exists a corresponding lowercase letter [CX] [Option Start] (as defined by character type information in the program locale category LC_CTYPE ), [Option End] the result shall be the corresponding lowercase letter. All other arguments in the domain are returned unchanged." If this is the case, where does tolower() fail?
    – 9a3eedi
    Mar 21, 2022 at 11:20
-1

I'm new to C and have been trying hard to convert strings from uppercase to lowercase. I made the mistake of changing the string 'A' + 32 = 'a'. And I can't solve the problem.

I used char type and finally I was able to convert it to string type. You can consult:

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

string convert_lower(string str)
{
    int length = strlen(str);
    char c[length + 1];

    for (int i = 0; i < length; i++)
    {
        if (str[i] <= 'Z' || str[i] >= 'A')
        {
            c[i] = tolower((char)str[i]);
        }
    }
    c[length] = '\0';
    string text = c;
    return text;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.