How can I convert a mixed case string to a lowercase string in C?
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);
for ( ; *p; ++p) *p = tolower(*p);
seems more idiomatic. –
Convulsion char *
, but not if str is a char array. Got any explanation for that? –
Profanity char *
you used really a string constant which gets placed in unwriteable memory section. So if you try to change the value of that it gives you a segfault. If, on the other hand, you would copy that constant string to a char *copy = strdup (const_string)
then copy
could be altered because it is allocated on the heap, which is writeable. So all in all it does not have anything to do with using a pointer (char *
) but with using constant strings like in code: char *const_str = "my const string"
. –
Tureen error: lvalue required as increment operand
–
Dahle while (*p = tolower(*p)) ++p;
or while (*p = tolower(*p++))
for the purpose of obfuscation (the latter only works since C++17) ;-) –
Jacks '\0'
numericaly represented as literal 0
. So when it encounters the last character as it increments, the loop terminates since it is falsy. I believe there are no ramifications NOP da CALL. –
Heathenish char
value before passing to tolower()
(which requires positive versions of characters, or EOF
). –
Rudolph 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;
for(char *p = pstr;*p;++p) *p=*p>='A'&&*p<='Z'?*p|0x60:*p;
–
Animosity tolower()
. 55.2 vs. 44.15 on my machine. –
Convulsion int (*)(int)
signature. Here's the code used for performance measurements gist.github.com/370497 –
Convulsion if (*p > 'Z')
optimization performs better on the input I've used, but if there are many upper-case letters it takes the same time as the previous version. –
Convulsion 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;
}
s
is a local variable in your function, you can directly use it instead of declaring p
.` –
Spiritualize 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.
You may use this user defined function:
char *ToLower(char *text){
int stringlength=strlen(text);
char *lower=(char*)malloc(sizeof(char)*stringlength);
int i=0;
while(i<stringlength){
//checking if the character is an uppercase
if((text[i]-'A')<=25){
lower[i]=text[i]+32;
}
//if the character is lowercase
else{
lower[i]=text[i];
}
i++;
}
return lower;
}
(text[i]-'A')<=25
can be true for values different than [A-Z] –
Subaquatic 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;
}
© 2022 - 2024 — McMap. All rights reserved.
strlwr((char*)str);
It just goes through the string and converts it itself. – Isocline