I need some help over here, this is my code for the program that adds 32 to a character so that the output will be lower case. But for what i have written, it seems that it doesnt work as planned. Hopefully you guys can help me to solve it, i cant really find where the fault is.. or at least give me some hint on it.. Thanks in advance..
#include<stdio.h>
#define MaxAry 50
int init_char(char cAry[], int charNum);
void modify_char(char cAry[]);
int main(void)
{
char cAry[MaxAry];
int i;
int cNum;
cNum = init_char(cAry, cNum);
modify_char(cAry);
for (i = 0; i < cNum; i++)
{
printf("The output alphabet at input %02d is %c\n", i + 1, cAry[i]);
}
}
int init_char(char cAry[], int charNum)
{
int size;
printf("Enter the number of alphabet you wish to put:\n");
scanf("%d", &size);
for (charNum = 0; charNum < size; charNum++)
{
printf("Enter alphabet %02d : \n", charNum + 1);
scanf("%c", &cAry[charNum]);
}
return charNum;
}
void modify_char(char cAry[])
{
int i;
for (i = 0; i < MaxAry; i++)
{
cAry[i] += 32;
}
}
If you wrote nice c++, the problem would (a) not have occurred (b) be solved by now, if it did anyways and (c) be a fraction of the size.
Here is some code which should do what you want:
1 2 3 4 5 6 7 8 9
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string s = "Hello, World!";
std::transform(s.begin(), s.end(), s.begin(), tolower);
std::cout<<s<<std::flush;
}
try this one its working dear enjoy:) for me i am using some online compiler so not able to use scanf u can use it
#include<stdio.h>
#define MaxAry 50
static int init_char(char cAry[]);
void modify_char(char cAry[]);
int main(void)
{
char cAry[MaxAry];
int i;
int cNum;
cNum = init_char(cAry);
modify_char(cAry);
for (i = 0; i < cNum; i++)
{
printf("The output alphabet at input %02d is %c\n", i + 1, cAry[i]);
}
}
static int init_char(char cAry[])
{
int charNum ;
int size=10;
//printf("Enter the number of alphabet you wish to put:\n");
//scanf("%d", &size);
for (charNum = 0; charNum < size; charNum++)
{
printf("Enter alphabet %02d : \n", charNum + 1);
scanf("%c", &cAry[charNum]);
}
return charNum;
}
void modify_char(char cAry[])
{
int i;
for (i = 0; i < MaxAry; i++)
{
cAry[i] += 32;
}
}
If you add 32, you are assuming that the user always enters upper case letters.
Try this:
1 2 3 4 5
void modify_char(char cAry[])
{
int i;
for (i = 0; i < MaxAry; i++) cAry[i] = cAry[i] & 32;
}
You are setting the 6th bit to 1 in any case which is the same as adding 32 to a upper-case letter but it will not affect a lower case letter where this bit is already set.
The more elegant way is simply to change the character_traits class (that is what it is there for, anyways). A std::string is just a typedef std::basic_string<char, std::char_traits<char>, std::allocator<char> >. Change the traits (whatever you want - should just the be comparison case insensitive or the assignment, too?) and you are done.