这题该怎么做,一点思路都没有
1、编写函数void fun(char * strFrom, char*strTo),其功能是:把字符串strFrom中的小写字母改成原字母的大写字母,大写字母改成原字母的小写字母,其它字母不变,其结果赋给指针strTo。如:输入字符串 a1234HnB则函数值应为 A1234hNb'X'+32='x',往ascii值的运算上想 ascii值判断呗 https://cdn.luogu.com.cn/upload/image_hosting/enm7ku7y.png
printf("%c",char(int(ch)-32));
//小写改大写
printf("%c",char(int(ch)+32));
//大写改小写 这个逻辑相对不难吧。对每个字符进行判断,如果在'a'与'z'之间,减指定值就行了。 遍历这个字符串 然后进行转换 赋值给你那个指针 #include <stdio.h>
void fun(char * strFrom, char*strTo)
{
char c;
while (*strFrom != 0)
{
c = *strFrom;
if (c >= 'A' && c <= 'Z')
{
*strTo = *strFrom + 0x20;
}
else if (c >= 'a' && c <= 'z')
{
*strTo = *strFrom - 0x20;
}
else
{
*strTo = *strFrom;
}
strFrom++;
strTo++;
}
}
int main()
{
char test[] = "a1234HnB";
char result = {0};
fun(test, result);
printf(result);
getchar();
return 0;
} 伪码:
ret="";
for int i=1 to len(str)
{
temp=str;
if asc(str)小写范围
temp=大写(str);
elseif asc(str)大写范围
temp=小写(str);
ret=ret+temp;
}
print ret; while(strFrom!=0){
*strTo=*strFrom;
if(!(tmp>='0'&&tmp<='9')){
*strTo^=0x20;
}
strTo++;
strFrom++;
}
不晓得对不对,应付题肯定没问题
// 正常思路版
void fun(const char* from, char* to)
{
int i = 0;
do
{
if ('A' <= from && from <= 'Z')
to = from - 'A' + 'a';
else if ('a' <= from && from <= 'z')
to = from - 'a' + 'A';
else
to = from;
} while (from != 0);
}
// 映射优化版
void fun_2(const char* input, char* output)
{
char map;
for (int i = 0; i < 256; ++i)
map = i;// 初始化映射
for (int i = 'A'; i <= 'Z'; ++i)// 大写换小写
map = i - 'A' + 'a';
for (int i = 'a'; i <= 'z'; ++i)// 小写换大写
map = i - 'a' + 'A';
int i = 0;
do {
output = map];
} while (input != 0);
}