converting a word to lowercase
Can anyone help me finish this?
void lowerCase(char ch, string array[])
{
int index;
ch = tolower(ch);
}
Hello,
Here is what u need;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
using namespace std;
char* toLowerCase(char* str)
{
int differ = 'A'-'a';
char ch;
int ii = strlen(str);
for (int i=0; i <ii;i++)
{
strncpy(&ch,str+i,1);
if (ch>='A' && ch<='Z')
{
ch = ch-differ;
memcpy(str+i,&ch,1);
}
}
return str;
}
int main()
{
char* pstr = (char*)calloc(100,sizeof(char));
string s;
s="TestAAAAAAAAA";
strcpy(pstr,"BBaaCCdRT");
printf("%s\n",toLowerCase(pstr));
strcpy(pstr,s.c_str());
printf("%s\n",toLowerCase(pstr));
getchar();
return 1;
}
|
Last edited on
If you want to use string as parametr,
this procedure helps you;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
string toLower(string strr)
{
char str[100];
string ret;
strcpy(str,strr.c_str());
int differ = 'A'-'a';
char ch;
int ii = strlen(str);
for (int i=0; i <ii;i++)
{
strncpy(&ch,str+i,1);
if (ch>='A' && ch<='Z')
{
ch = ch-differ;
memcpy(str+i,&ch,1);
}
}
ret = str;
return ret;
}
|
Last edited on
Here are two ways to convert a string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
//
// The For loop way
void toLowerCase(std::string &str)
{
const int length = str.length();
for(int i=0; i < length; ++i)
{
str[i] = std::tolower(str[i]);
}
}
//
//The STL algorithm way
void toLowerCaseSTD(std::string &str)
{
std::transform(str.begin(), str.end(), str.begin(), std::tolower);
}
int main()
{
std::string test("ABCDEFG");
toLowerCase(test);
std::cout << test << std::endl;
std::string test2("HIJKLMNO");
toLowerCaseSTD(test2);
std::cout << test2 << std::endl;
}
|
Last edited on
Topic archived. No new replies allowed.