Need assistance with converting const char* word to a lowercase version of it2019 Community Moderator ElectionWhy can't I convert 'char**' to a 'const char* const*' in C?What is the difference between char * const and const char *?Why is it not OK to pass `char **` to a function that takes a `const char **` in C?cannot convert `char (*)[((unsigned int)((int)Tlength))]' to `char**Incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *'unsigned int to const char* in CConverting array of char pointers to lowercase CC - converting a uppercase char to lowercase charC - Convert const char hex string to unsigned charWarning: while trying to convert java byte[] to C unsigned char*

Why is "la Gestapo" feminine?

Friend wants my recommendation but I don't want to

Air travel with refrigerated insulin

Was World War I a war of liberals against authoritarians?

How to test the sharpness of a knife?

Turning a hard to access nut?

Fair way to split coins

Why does Surtur say that Thor is Asgard's doom?

Print last inputted byte

Emojional cryptic crossword

How to balance a monster modification (zombie)?

Animating wave motion in water

Does fire aspect on a sword, destroy mob drops?

Is "inadequate referencing" a euphemism for plagiarism?

Hackerrank All Women's Codesprint 2019: Name the Product

Does the Shadow Magic sorcerer's Eyes of the Dark feature work on all Darkness spells or just his/her own?

Symbolism of 18 Journeyers

Do people actually use the word "kaputt" in conversation?

label a part of commutative diagram

How do you justify more code being written by following clean code practices?

Why I don't get the wanted width of tcbox?

is this saw blade faulty?

Error in master's thesis, I do not know what to do

When did hardware antialiasing start being available?



Need assistance with converting const char* word to a lowercase version of it



2019 Community Moderator ElectionWhy can't I convert 'char**' to a 'const char* const*' in C?What is the difference between char * const and const char *?Why is it not OK to pass `char **` to a function that takes a `const char **` in C?cannot convert `char (*)[((unsigned int)((int)Tlength))]' to `char**Incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *'unsigned int to const char* in CConverting array of char pointers to lowercase CC - converting a uppercase char to lowercase charC - Convert const char hex string to unsigned charWarning: while trying to convert java byte[] to C unsigned char*










1















I am trying to convert a a const.char to a lowercase version of that word. Here is the code I currently I have:



int i=0;
char DuplicateArray[45];
int sizevalue=0;
Node* NodePointer=NULL;
unsigned int hashval=0;
int counter=0;
sizevalue=strlen (word);

strncpy(&DuplicateArray[counter], word,sizevalue);//word is the const char pointer.
DuplicateArray[sizevalue+1] = '';
hashval=hash(DuplicateArray);//function I call to determine hash value
while ( DuplicateArray[i] != '' )

DuplicateArray[i] = tolower(DuplicateArray[i]);
i++;



With this code I have, however, I am not able to make the characters in the array lower cased. Does anyone have any idea on what I am doing wrong?










share|improve this question



















  • 1





    How is DuplicateArray declared ?

    – cnicutar
    Mar 6 '13 at 7:27











  • Like this: char DuplicateArray[45];

    – user2014904
    Mar 6 '13 at 7:28






  • 1





    How are counter and i initialized?

    – Henrik
    Mar 6 '13 at 7:29











  • I just updated it...

    – user2014904
    Mar 6 '13 at 7:31











  • Can I assume word is declared as const char *word?

    – Anish Ramaswamy
    Mar 6 '13 at 7:36















1















I am trying to convert a a const.char to a lowercase version of that word. Here is the code I currently I have:



int i=0;
char DuplicateArray[45];
int sizevalue=0;
Node* NodePointer=NULL;
unsigned int hashval=0;
int counter=0;
sizevalue=strlen (word);

strncpy(&DuplicateArray[counter], word,sizevalue);//word is the const char pointer.
DuplicateArray[sizevalue+1] = '';
hashval=hash(DuplicateArray);//function I call to determine hash value
while ( DuplicateArray[i] != '' )

DuplicateArray[i] = tolower(DuplicateArray[i]);
i++;



With this code I have, however, I am not able to make the characters in the array lower cased. Does anyone have any idea on what I am doing wrong?










share|improve this question



















  • 1





    How is DuplicateArray declared ?

    – cnicutar
    Mar 6 '13 at 7:27











  • Like this: char DuplicateArray[45];

    – user2014904
    Mar 6 '13 at 7:28






  • 1





    How are counter and i initialized?

    – Henrik
    Mar 6 '13 at 7:29











  • I just updated it...

    – user2014904
    Mar 6 '13 at 7:31











  • Can I assume word is declared as const char *word?

    – Anish Ramaswamy
    Mar 6 '13 at 7:36













1












1








1








I am trying to convert a a const.char to a lowercase version of that word. Here is the code I currently I have:



int i=0;
char DuplicateArray[45];
int sizevalue=0;
Node* NodePointer=NULL;
unsigned int hashval=0;
int counter=0;
sizevalue=strlen (word);

strncpy(&DuplicateArray[counter], word,sizevalue);//word is the const char pointer.
DuplicateArray[sizevalue+1] = '';
hashval=hash(DuplicateArray);//function I call to determine hash value
while ( DuplicateArray[i] != '' )

DuplicateArray[i] = tolower(DuplicateArray[i]);
i++;



With this code I have, however, I am not able to make the characters in the array lower cased. Does anyone have any idea on what I am doing wrong?










share|improve this question
















I am trying to convert a a const.char to a lowercase version of that word. Here is the code I currently I have:



int i=0;
char DuplicateArray[45];
int sizevalue=0;
Node* NodePointer=NULL;
unsigned int hashval=0;
int counter=0;
sizevalue=strlen (word);

strncpy(&DuplicateArray[counter], word,sizevalue);//word is the const char pointer.
DuplicateArray[sizevalue+1] = '';
hashval=hash(DuplicateArray);//function I call to determine hash value
while ( DuplicateArray[i] != '' )

DuplicateArray[i] = tolower(DuplicateArray[i]);
i++;



With this code I have, however, I am not able to make the characters in the array lower cased. Does anyone have any idea on what I am doing wrong?







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 '13 at 8:24









Henrik

21.3k53485




21.3k53485










asked Mar 6 '13 at 7:26









user2014904user2014904

418




418







  • 1





    How is DuplicateArray declared ?

    – cnicutar
    Mar 6 '13 at 7:27











  • Like this: char DuplicateArray[45];

    – user2014904
    Mar 6 '13 at 7:28






  • 1





    How are counter and i initialized?

    – Henrik
    Mar 6 '13 at 7:29











  • I just updated it...

    – user2014904
    Mar 6 '13 at 7:31











  • Can I assume word is declared as const char *word?

    – Anish Ramaswamy
    Mar 6 '13 at 7:36












  • 1





    How is DuplicateArray declared ?

    – cnicutar
    Mar 6 '13 at 7:27











  • Like this: char DuplicateArray[45];

    – user2014904
    Mar 6 '13 at 7:28






  • 1





    How are counter and i initialized?

    – Henrik
    Mar 6 '13 at 7:29











  • I just updated it...

    – user2014904
    Mar 6 '13 at 7:31











  • Can I assume word is declared as const char *word?

    – Anish Ramaswamy
    Mar 6 '13 at 7:36







1




1





How is DuplicateArray declared ?

– cnicutar
Mar 6 '13 at 7:27





How is DuplicateArray declared ?

– cnicutar
Mar 6 '13 at 7:27













Like this: char DuplicateArray[45];

– user2014904
Mar 6 '13 at 7:28





Like this: char DuplicateArray[45];

– user2014904
Mar 6 '13 at 7:28




1




1





How are counter and i initialized?

– Henrik
Mar 6 '13 at 7:29





How are counter and i initialized?

– Henrik
Mar 6 '13 at 7:29













I just updated it...

– user2014904
Mar 6 '13 at 7:31





I just updated it...

– user2014904
Mar 6 '13 at 7:31













Can I assume word is declared as const char *word?

– Anish Ramaswamy
Mar 6 '13 at 7:36





Can I assume word is declared as const char *word?

– Anish Ramaswamy
Mar 6 '13 at 7:36












3 Answers
3






active

oldest

votes


















2














My guess is that you're either misunderstanding arrays and the & and [] operators, or the other various "small" mistakes in your code (that all result in UB as far as I can tell) make your program misbehave. This works:



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

int main()

const char* uppercase = "UpPeRcAsE";

char duplicateArray[45];
int uppercaseSize = strlen(uppercase);

// copy uppercase into duplicateArray
strncpy(duplicateArray, uppercase, uppercaseSize);

duplicateArray[uppercaseSize] = '';
int i = 0;
while (duplicateArray[i] != '')

duplicateArray[i] = tolower(duplicateArray[i]);
i++;


printf("Before: %s, after: %sn", uppercase, duplicateArray);

return 0;






share|improve this answer

























  • Btw, you better indent and format your code properly, use lowercase and mixed case identifiers consistently and read up about strings more - the code is horrible as currently standing.

    – user529758
    Mar 6 '13 at 7:42











  • ok...thats good to know...so its probably something else thats not ticking thats causing me to go gray is what your saying?

    – user2014904
    Mar 6 '13 at 7:43











  • @user2014904 It may be... As I explained, you seem to have some misunderstanding about arrays or strings or both.

    – user529758
    Mar 6 '13 at 7:45











  • Cool. thanks man!I appreciate. Bitter sweet. Didnt find the problem, but what I was doing wasnt totally off either. And thanks for the tip on formatting and what not. Truth hurts, but better know now then later..

    – user2014904
    Mar 6 '13 at 7:50


















0














If the encoding is Ascii, do it in binary. The difference is in bit 6, which is 0 in uppercase letters and 1 in lowercase letters. You can easily find do it by subtracting 32 in decimal, or just flip the sixth bit. For example, character A is 1000001 (0x41) and character a is 1100001 (0x61).






share|improve this answer


















  • 2





    Nah, advising OP to hack with non-portable hardcoded values instead of standard library macros is plain wrong.

    – user529758
    Mar 6 '13 at 7:32











  • H2CO3 is on a downvoting spree, somebody stop him. But actually you don't need to do binary, you can do tolower, or simply check ranges and use the fact that ("Z"-"A")+"a" = "z"

    – Dmitry
    Mar 6 '13 at 7:41






  • 1





    @Dmitry I'm not on a downvoting spree, it's just that both answers are wrong.

    – user529758
    Mar 6 '13 at 7:43











  • Correct. ("Z"-"A")+"a" = "z" is neat.

    – meyumer
    Mar 6 '13 at 7:43






  • 1





    @Dmitry There is: wrong answers aren't useful.

    – user529758
    Mar 6 '13 at 7:44


















-1














use this



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

void dotolower(const char *cstr)

char *str = (char*)cstr;

while (*str)
*str = tolower(*str);
++str;



int main()

char mystr[] = "HELLO";
dotolower(mystr);
puts(mystr);

return 0;



as an example






share|improve this answer

























  • and yes, this example is evil because the function lies about not touching the string passed.

    – Dmitry
    Mar 6 '13 at 7:35






  • 2





    which you should never do (because it's a constraint violation therefore it invokes undefined behavior).

    – user529758
    Mar 6 '13 at 7:37











  • yes, thats exactly why I used mystr[] rather than mystr*, mystr* would, and actually did crash when I tested this program. This problem isn't safe altogether. But an analogy is an analogy.

    – Dmitry
    Mar 6 '13 at 7:39











  • Why not just take cstr by non-const pointer?

    – HighCommander4
    Mar 6 '13 at 8:13











  • You can, but as depth of pointers increases, that solution makes code messy. I believe doing such things does not offer a great demonstrational value.

    – Dmitry
    Mar 6 '13 at 8:21










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15241380%2fneed-assistance-with-converting-const-char-word-to-a-lowercase-version-of-it%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














My guess is that you're either misunderstanding arrays and the & and [] operators, or the other various "small" mistakes in your code (that all result in UB as far as I can tell) make your program misbehave. This works:



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

int main()

const char* uppercase = "UpPeRcAsE";

char duplicateArray[45];
int uppercaseSize = strlen(uppercase);

// copy uppercase into duplicateArray
strncpy(duplicateArray, uppercase, uppercaseSize);

duplicateArray[uppercaseSize] = '';
int i = 0;
while (duplicateArray[i] != '')

duplicateArray[i] = tolower(duplicateArray[i]);
i++;


printf("Before: %s, after: %sn", uppercase, duplicateArray);

return 0;






share|improve this answer

























  • Btw, you better indent and format your code properly, use lowercase and mixed case identifiers consistently and read up about strings more - the code is horrible as currently standing.

    – user529758
    Mar 6 '13 at 7:42











  • ok...thats good to know...so its probably something else thats not ticking thats causing me to go gray is what your saying?

    – user2014904
    Mar 6 '13 at 7:43











  • @user2014904 It may be... As I explained, you seem to have some misunderstanding about arrays or strings or both.

    – user529758
    Mar 6 '13 at 7:45











  • Cool. thanks man!I appreciate. Bitter sweet. Didnt find the problem, but what I was doing wasnt totally off either. And thanks for the tip on formatting and what not. Truth hurts, but better know now then later..

    – user2014904
    Mar 6 '13 at 7:50















2














My guess is that you're either misunderstanding arrays and the & and [] operators, or the other various "small" mistakes in your code (that all result in UB as far as I can tell) make your program misbehave. This works:



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

int main()

const char* uppercase = "UpPeRcAsE";

char duplicateArray[45];
int uppercaseSize = strlen(uppercase);

// copy uppercase into duplicateArray
strncpy(duplicateArray, uppercase, uppercaseSize);

duplicateArray[uppercaseSize] = '';
int i = 0;
while (duplicateArray[i] != '')

duplicateArray[i] = tolower(duplicateArray[i]);
i++;


printf("Before: %s, after: %sn", uppercase, duplicateArray);

return 0;






share|improve this answer

























  • Btw, you better indent and format your code properly, use lowercase and mixed case identifiers consistently and read up about strings more - the code is horrible as currently standing.

    – user529758
    Mar 6 '13 at 7:42











  • ok...thats good to know...so its probably something else thats not ticking thats causing me to go gray is what your saying?

    – user2014904
    Mar 6 '13 at 7:43











  • @user2014904 It may be... As I explained, you seem to have some misunderstanding about arrays or strings or both.

    – user529758
    Mar 6 '13 at 7:45











  • Cool. thanks man!I appreciate. Bitter sweet. Didnt find the problem, but what I was doing wasnt totally off either. And thanks for the tip on formatting and what not. Truth hurts, but better know now then later..

    – user2014904
    Mar 6 '13 at 7:50













2












2








2







My guess is that you're either misunderstanding arrays and the & and [] operators, or the other various "small" mistakes in your code (that all result in UB as far as I can tell) make your program misbehave. This works:



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

int main()

const char* uppercase = "UpPeRcAsE";

char duplicateArray[45];
int uppercaseSize = strlen(uppercase);

// copy uppercase into duplicateArray
strncpy(duplicateArray, uppercase, uppercaseSize);

duplicateArray[uppercaseSize] = '';
int i = 0;
while (duplicateArray[i] != '')

duplicateArray[i] = tolower(duplicateArray[i]);
i++;


printf("Before: %s, after: %sn", uppercase, duplicateArray);

return 0;






share|improve this answer















My guess is that you're either misunderstanding arrays and the & and [] operators, or the other various "small" mistakes in your code (that all result in UB as far as I can tell) make your program misbehave. This works:



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

int main()

const char* uppercase = "UpPeRcAsE";

char duplicateArray[45];
int uppercaseSize = strlen(uppercase);

// copy uppercase into duplicateArray
strncpy(duplicateArray, uppercase, uppercaseSize);

duplicateArray[uppercaseSize] = '';
int i = 0;
while (duplicateArray[i] != '')

duplicateArray[i] = tolower(duplicateArray[i]);
i++;


printf("Before: %s, after: %sn", uppercase, duplicateArray);

return 0;







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 6 at 23:21









Olivia Stork

3,22241534




3,22241534










answered Mar 6 '13 at 7:39







user529758



















  • Btw, you better indent and format your code properly, use lowercase and mixed case identifiers consistently and read up about strings more - the code is horrible as currently standing.

    – user529758
    Mar 6 '13 at 7:42











  • ok...thats good to know...so its probably something else thats not ticking thats causing me to go gray is what your saying?

    – user2014904
    Mar 6 '13 at 7:43











  • @user2014904 It may be... As I explained, you seem to have some misunderstanding about arrays or strings or both.

    – user529758
    Mar 6 '13 at 7:45











  • Cool. thanks man!I appreciate. Bitter sweet. Didnt find the problem, but what I was doing wasnt totally off either. And thanks for the tip on formatting and what not. Truth hurts, but better know now then later..

    – user2014904
    Mar 6 '13 at 7:50

















  • Btw, you better indent and format your code properly, use lowercase and mixed case identifiers consistently and read up about strings more - the code is horrible as currently standing.

    – user529758
    Mar 6 '13 at 7:42











  • ok...thats good to know...so its probably something else thats not ticking thats causing me to go gray is what your saying?

    – user2014904
    Mar 6 '13 at 7:43











  • @user2014904 It may be... As I explained, you seem to have some misunderstanding about arrays or strings or both.

    – user529758
    Mar 6 '13 at 7:45











  • Cool. thanks man!I appreciate. Bitter sweet. Didnt find the problem, but what I was doing wasnt totally off either. And thanks for the tip on formatting and what not. Truth hurts, but better know now then later..

    – user2014904
    Mar 6 '13 at 7:50
















Btw, you better indent and format your code properly, use lowercase and mixed case identifiers consistently and read up about strings more - the code is horrible as currently standing.

– user529758
Mar 6 '13 at 7:42





Btw, you better indent and format your code properly, use lowercase and mixed case identifiers consistently and read up about strings more - the code is horrible as currently standing.

– user529758
Mar 6 '13 at 7:42













ok...thats good to know...so its probably something else thats not ticking thats causing me to go gray is what your saying?

– user2014904
Mar 6 '13 at 7:43





ok...thats good to know...so its probably something else thats not ticking thats causing me to go gray is what your saying?

– user2014904
Mar 6 '13 at 7:43













@user2014904 It may be... As I explained, you seem to have some misunderstanding about arrays or strings or both.

– user529758
Mar 6 '13 at 7:45





@user2014904 It may be... As I explained, you seem to have some misunderstanding about arrays or strings or both.

– user529758
Mar 6 '13 at 7:45













Cool. thanks man!I appreciate. Bitter sweet. Didnt find the problem, but what I was doing wasnt totally off either. And thanks for the tip on formatting and what not. Truth hurts, but better know now then later..

– user2014904
Mar 6 '13 at 7:50





Cool. thanks man!I appreciate. Bitter sweet. Didnt find the problem, but what I was doing wasnt totally off either. And thanks for the tip on formatting and what not. Truth hurts, but better know now then later..

– user2014904
Mar 6 '13 at 7:50













0














If the encoding is Ascii, do it in binary. The difference is in bit 6, which is 0 in uppercase letters and 1 in lowercase letters. You can easily find do it by subtracting 32 in decimal, or just flip the sixth bit. For example, character A is 1000001 (0x41) and character a is 1100001 (0x61).






share|improve this answer


















  • 2





    Nah, advising OP to hack with non-portable hardcoded values instead of standard library macros is plain wrong.

    – user529758
    Mar 6 '13 at 7:32











  • H2CO3 is on a downvoting spree, somebody stop him. But actually you don't need to do binary, you can do tolower, or simply check ranges and use the fact that ("Z"-"A")+"a" = "z"

    – Dmitry
    Mar 6 '13 at 7:41






  • 1





    @Dmitry I'm not on a downvoting spree, it's just that both answers are wrong.

    – user529758
    Mar 6 '13 at 7:43











  • Correct. ("Z"-"A")+"a" = "z" is neat.

    – meyumer
    Mar 6 '13 at 7:43






  • 1





    @Dmitry There is: wrong answers aren't useful.

    – user529758
    Mar 6 '13 at 7:44















0














If the encoding is Ascii, do it in binary. The difference is in bit 6, which is 0 in uppercase letters and 1 in lowercase letters. You can easily find do it by subtracting 32 in decimal, or just flip the sixth bit. For example, character A is 1000001 (0x41) and character a is 1100001 (0x61).






share|improve this answer


















  • 2





    Nah, advising OP to hack with non-portable hardcoded values instead of standard library macros is plain wrong.

    – user529758
    Mar 6 '13 at 7:32











  • H2CO3 is on a downvoting spree, somebody stop him. But actually you don't need to do binary, you can do tolower, or simply check ranges and use the fact that ("Z"-"A")+"a" = "z"

    – Dmitry
    Mar 6 '13 at 7:41






  • 1





    @Dmitry I'm not on a downvoting spree, it's just that both answers are wrong.

    – user529758
    Mar 6 '13 at 7:43











  • Correct. ("Z"-"A")+"a" = "z" is neat.

    – meyumer
    Mar 6 '13 at 7:43






  • 1





    @Dmitry There is: wrong answers aren't useful.

    – user529758
    Mar 6 '13 at 7:44













0












0








0







If the encoding is Ascii, do it in binary. The difference is in bit 6, which is 0 in uppercase letters and 1 in lowercase letters. You can easily find do it by subtracting 32 in decimal, or just flip the sixth bit. For example, character A is 1000001 (0x41) and character a is 1100001 (0x61).






share|improve this answer













If the encoding is Ascii, do it in binary. The difference is in bit 6, which is 0 in uppercase letters and 1 in lowercase letters. You can easily find do it by subtracting 32 in decimal, or just flip the sixth bit. For example, character A is 1000001 (0x41) and character a is 1100001 (0x61).







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 6 '13 at 7:31









meyumermeyumer

4,37211121




4,37211121







  • 2





    Nah, advising OP to hack with non-portable hardcoded values instead of standard library macros is plain wrong.

    – user529758
    Mar 6 '13 at 7:32











  • H2CO3 is on a downvoting spree, somebody stop him. But actually you don't need to do binary, you can do tolower, or simply check ranges and use the fact that ("Z"-"A")+"a" = "z"

    – Dmitry
    Mar 6 '13 at 7:41






  • 1





    @Dmitry I'm not on a downvoting spree, it's just that both answers are wrong.

    – user529758
    Mar 6 '13 at 7:43











  • Correct. ("Z"-"A")+"a" = "z" is neat.

    – meyumer
    Mar 6 '13 at 7:43






  • 1





    @Dmitry There is: wrong answers aren't useful.

    – user529758
    Mar 6 '13 at 7:44












  • 2





    Nah, advising OP to hack with non-portable hardcoded values instead of standard library macros is plain wrong.

    – user529758
    Mar 6 '13 at 7:32











  • H2CO3 is on a downvoting spree, somebody stop him. But actually you don't need to do binary, you can do tolower, or simply check ranges and use the fact that ("Z"-"A")+"a" = "z"

    – Dmitry
    Mar 6 '13 at 7:41






  • 1





    @Dmitry I'm not on a downvoting spree, it's just that both answers are wrong.

    – user529758
    Mar 6 '13 at 7:43











  • Correct. ("Z"-"A")+"a" = "z" is neat.

    – meyumer
    Mar 6 '13 at 7:43






  • 1





    @Dmitry There is: wrong answers aren't useful.

    – user529758
    Mar 6 '13 at 7:44







2




2





Nah, advising OP to hack with non-portable hardcoded values instead of standard library macros is plain wrong.

– user529758
Mar 6 '13 at 7:32





Nah, advising OP to hack with non-portable hardcoded values instead of standard library macros is plain wrong.

– user529758
Mar 6 '13 at 7:32













H2CO3 is on a downvoting spree, somebody stop him. But actually you don't need to do binary, you can do tolower, or simply check ranges and use the fact that ("Z"-"A")+"a" = "z"

– Dmitry
Mar 6 '13 at 7:41





H2CO3 is on a downvoting spree, somebody stop him. But actually you don't need to do binary, you can do tolower, or simply check ranges and use the fact that ("Z"-"A")+"a" = "z"

– Dmitry
Mar 6 '13 at 7:41




1




1





@Dmitry I'm not on a downvoting spree, it's just that both answers are wrong.

– user529758
Mar 6 '13 at 7:43





@Dmitry I'm not on a downvoting spree, it's just that both answers are wrong.

– user529758
Mar 6 '13 at 7:43













Correct. ("Z"-"A")+"a" = "z" is neat.

– meyumer
Mar 6 '13 at 7:43





Correct. ("Z"-"A")+"a" = "z" is neat.

– meyumer
Mar 6 '13 at 7:43




1




1





@Dmitry There is: wrong answers aren't useful.

– user529758
Mar 6 '13 at 7:44





@Dmitry There is: wrong answers aren't useful.

– user529758
Mar 6 '13 at 7:44











-1














use this



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

void dotolower(const char *cstr)

char *str = (char*)cstr;

while (*str)
*str = tolower(*str);
++str;



int main()

char mystr[] = "HELLO";
dotolower(mystr);
puts(mystr);

return 0;



as an example






share|improve this answer

























  • and yes, this example is evil because the function lies about not touching the string passed.

    – Dmitry
    Mar 6 '13 at 7:35






  • 2





    which you should never do (because it's a constraint violation therefore it invokes undefined behavior).

    – user529758
    Mar 6 '13 at 7:37











  • yes, thats exactly why I used mystr[] rather than mystr*, mystr* would, and actually did crash when I tested this program. This problem isn't safe altogether. But an analogy is an analogy.

    – Dmitry
    Mar 6 '13 at 7:39











  • Why not just take cstr by non-const pointer?

    – HighCommander4
    Mar 6 '13 at 8:13











  • You can, but as depth of pointers increases, that solution makes code messy. I believe doing such things does not offer a great demonstrational value.

    – Dmitry
    Mar 6 '13 at 8:21















-1














use this



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

void dotolower(const char *cstr)

char *str = (char*)cstr;

while (*str)
*str = tolower(*str);
++str;



int main()

char mystr[] = "HELLO";
dotolower(mystr);
puts(mystr);

return 0;



as an example






share|improve this answer

























  • and yes, this example is evil because the function lies about not touching the string passed.

    – Dmitry
    Mar 6 '13 at 7:35






  • 2





    which you should never do (because it's a constraint violation therefore it invokes undefined behavior).

    – user529758
    Mar 6 '13 at 7:37











  • yes, thats exactly why I used mystr[] rather than mystr*, mystr* would, and actually did crash when I tested this program. This problem isn't safe altogether. But an analogy is an analogy.

    – Dmitry
    Mar 6 '13 at 7:39











  • Why not just take cstr by non-const pointer?

    – HighCommander4
    Mar 6 '13 at 8:13











  • You can, but as depth of pointers increases, that solution makes code messy. I believe doing such things does not offer a great demonstrational value.

    – Dmitry
    Mar 6 '13 at 8:21













-1












-1








-1







use this



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

void dotolower(const char *cstr)

char *str = (char*)cstr;

while (*str)
*str = tolower(*str);
++str;



int main()

char mystr[] = "HELLO";
dotolower(mystr);
puts(mystr);

return 0;



as an example






share|improve this answer















use this



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

void dotolower(const char *cstr)

char *str = (char*)cstr;

while (*str)
*str = tolower(*str);
++str;



int main()

char mystr[] = "HELLO";
dotolower(mystr);
puts(mystr);

return 0;



as an example







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 6 '13 at 9:57

























answered Mar 6 '13 at 7:34









DmitryDmitry

2,66232035




2,66232035












  • and yes, this example is evil because the function lies about not touching the string passed.

    – Dmitry
    Mar 6 '13 at 7:35






  • 2





    which you should never do (because it's a constraint violation therefore it invokes undefined behavior).

    – user529758
    Mar 6 '13 at 7:37











  • yes, thats exactly why I used mystr[] rather than mystr*, mystr* would, and actually did crash when I tested this program. This problem isn't safe altogether. But an analogy is an analogy.

    – Dmitry
    Mar 6 '13 at 7:39











  • Why not just take cstr by non-const pointer?

    – HighCommander4
    Mar 6 '13 at 8:13











  • You can, but as depth of pointers increases, that solution makes code messy. I believe doing such things does not offer a great demonstrational value.

    – Dmitry
    Mar 6 '13 at 8:21

















  • and yes, this example is evil because the function lies about not touching the string passed.

    – Dmitry
    Mar 6 '13 at 7:35






  • 2





    which you should never do (because it's a constraint violation therefore it invokes undefined behavior).

    – user529758
    Mar 6 '13 at 7:37











  • yes, thats exactly why I used mystr[] rather than mystr*, mystr* would, and actually did crash when I tested this program. This problem isn't safe altogether. But an analogy is an analogy.

    – Dmitry
    Mar 6 '13 at 7:39











  • Why not just take cstr by non-const pointer?

    – HighCommander4
    Mar 6 '13 at 8:13











  • You can, but as depth of pointers increases, that solution makes code messy. I believe doing such things does not offer a great demonstrational value.

    – Dmitry
    Mar 6 '13 at 8:21
















and yes, this example is evil because the function lies about not touching the string passed.

– Dmitry
Mar 6 '13 at 7:35





and yes, this example is evil because the function lies about not touching the string passed.

– Dmitry
Mar 6 '13 at 7:35




2




2





which you should never do (because it's a constraint violation therefore it invokes undefined behavior).

– user529758
Mar 6 '13 at 7:37





which you should never do (because it's a constraint violation therefore it invokes undefined behavior).

– user529758
Mar 6 '13 at 7:37













yes, thats exactly why I used mystr[] rather than mystr*, mystr* would, and actually did crash when I tested this program. This problem isn't safe altogether. But an analogy is an analogy.

– Dmitry
Mar 6 '13 at 7:39





yes, thats exactly why I used mystr[] rather than mystr*, mystr* would, and actually did crash when I tested this program. This problem isn't safe altogether. But an analogy is an analogy.

– Dmitry
Mar 6 '13 at 7:39













Why not just take cstr by non-const pointer?

– HighCommander4
Mar 6 '13 at 8:13





Why not just take cstr by non-const pointer?

– HighCommander4
Mar 6 '13 at 8:13













You can, but as depth of pointers increases, that solution makes code messy. I believe doing such things does not offer a great demonstrational value.

– Dmitry
Mar 6 '13 at 8:21





You can, but as depth of pointers increases, that solution makes code messy. I believe doing such things does not offer a great demonstrational value.

– Dmitry
Mar 6 '13 at 8:21

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15241380%2fneed-assistance-with-converting-const-char-word-to-a-lowercase-version-of-it%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved