Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a string to all lowercase [duplicate]

I have a char foo[SIZE]; //(string)

and have inputed it correctly using %s (as in it printfs the correct input), but now want to set it to lowercase. So I tried using

 if (isupper(*foo)) 
   *foo=tolower(*foo); 

ie when I do:

printf("%s" foo); //I get the same text with upper case

The text does not seem to change. Thank you.

like image 641
user2450044 Avatar asked Jun 04 '13 03:06

user2450044


People also ask

How do I make everything lowercase in a string?

The toLowerCase method converts a string to lowercase letters. The toLowerCase() method doesn't take in any parameters. Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new one that consists of only lowercase letters and returns that value.

How do I convert a string to lowercase to the first letter?

Or using a char array String input = "SomeInputString"; char c[] = input. toCharArray(); c[0] = Character. toLowerCase(c[0]); String output = new String(c);

How do you convert a string to a lowercase character?

The method toLowerCase() converts the characters of a String into lower case characters. It has two variants: String toLowerCase(Locale locale) : It converts the string into Lowercase using the rules defined by specified Locale. String toLowerCase() : It is equivalent to toLowerCase(Locale.

Which method is used to lowercase all the characters in a string in JavaScript?

The toLowerCase() method returns the value of the string converted to lower case. toLowerCase() does not affect the value of the string str itself.


3 Answers

foo isn't a pointer, so you don't want to use it as one. You also don't have to check whether a character is an upper-case letter before using tolower -- it converts upper to lower case, and leaves other characters unchanged. You probably want something like:

for (i=0; foo[i]; i++)
    foo[i] = tolower((unsigned char)foo[i]);

Note that when you call tolower (and toupper, isalpha, etc.) you really need to cast your input to unsigned char. Otherwise, many (most?) characters outside the basic English/ASCII character set will frequently lead to undefined behavior (e.g., in a typical case, most accented characters will show up as negative numbers).

As an aside, when you're reading the string, you don't want to use scanf with %s -- you always want to specify the string length, something like: scanf("%19s", foo);, assuming SIZE == 20 (i.e., you want to specify one less than the size. Alternatively, you could use fgets, like fgets(foo, 20, infile);. Note that with fgets, you specify the size of the buffer, not one less like you do with scanf (and company like fscanf).

like image 168
Jerry Coffin Avatar answered Oct 12 '22 22:10

Jerry Coffin


Try this

for(i = 0; foo[i]; i++){
  foo[i] = tolower(foo[i]);
}
like image 3
Bill Avatar answered Oct 12 '22 23:10

Bill


*foo=tolower(*foo); //doing *(foo+i) or foo[i] does not work either

because all of those options do not make sense

You should use it like this:

for(i = 0; foo[i] != '\0'; i++){
    foo[i] = tolower(foo[i]);
}
like image 3
Grzegorz Piwowarek Avatar answered Oct 12 '22 22:10

Grzegorz Piwowarek