c++ code
-
a code to change string to uppercase using integer values within the code.
-
a code to change string to uppercase using integer values within the code.
-
a code to change string to uppercase using integer values within the code.
Hint: There's an offset of 32 between lower and upper cased letters. For example, the ascii value (int) of lower case 'c' is 99, and the same of upper case 'C' is 67.
"Real men drive manual transmission" - Rajesh.
-
a code to change string to uppercase using integer values within the code.
The integer difference between a lowercase char and a uppercase char when subtracted is 32. simply subtract the lowercase by 32 to get a uppercase char.
#include #include int main(void)
{
char strchr[32] = "the quick fox";
int strlength=strlen(strchr);printf("Before\\n" ); printf("%s\\n\\n", strchr); for (int i=0; i\= 97) && (strchr\[i\] <= 122)) { strchr\[i\] = strchr\[i\] -32; } } printf("After\\n" ); printf("%s\\n\\n", strchr);
return(0);
}
-
The integer difference between a lowercase char and a uppercase char when subtracted is 32. simply subtract the lowercase by 32 to get a uppercase char.
#include #include int main(void)
{
char strchr[32] = "the quick fox";
int strlength=strlen(strchr);printf("Before\\n" ); printf("%s\\n\\n", strchr); for (int i=0; i\= 97) && (strchr\[i\] <= 122)) { strchr\[i\] = strchr\[i\] -32; } } printf("After\\n" ); printf("%s\\n\\n", strchr);
return(0);
}
-
The integer difference between a lowercase char and a uppercase char when subtracted is 32. simply subtract the lowercase by 32 to get a uppercase char.
#include #include int main(void)
{
char strchr[32] = "the quick fox";
int strlength=strlen(strchr);printf("Before\\n" ); printf("%s\\n\\n", strchr); for (int i=0; i\= 97) && (strchr\[i\] <= 122)) { strchr\[i\] = strchr\[i\] -32; } } printf("After\\n" ); printf("%s\\n\\n", strchr);
return(0);
}
TopCoder23 wrote:
char strchr[32] = "the quick fox";
I would consider a different variable name as
strchr
is the name of a CRT function.TopCoder23 wrote:
if((strchr[i] >= 97) && (strchr[i] <= 122))
What about:
if (islower(strchr[i]))
strchr[i] = toupper(strchr[i]);"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
TopCoder23 wrote:
char strchr[32] = "the quick fox";
I would consider a different variable name as
strchr
is the name of a CRT function.TopCoder23 wrote:
if((strchr[i] >= 97) && (strchr[i] <= 122))
What about:
if (islower(strchr[i]))
strchr[i] = toupper(strchr[i]);"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
You're right about
strchr()
. andislower()
. http://www.cplusplus.com/reference/clibrary/cstring/strchr/[^]