int main()
{
char ch[100];
printf("please input char:\n");
for (int i = 0; i < 100; i++)
{
scanf_s("%c", ch[i]);
}
for (int i = 0; i < 100; i++)
{
if ('a' <= ch[i] && ch[i] <= 'z')
{
ch[i] = ch[i] - 32;
}
else if ('A' <= ch[i] && ch[i] <= 'Z')
{
ch[i] = ch[i] + 32;
}
}
for (int i = 0; i < 100; i++)
{
printf("%c", ch[i]);
}
return 0;
}
必须用scanf printf 输入输出
下面的代码有毛病
scanf_s("%c", ch[i]);
改为
scanf_s("%c", &ch[i]);
学会函数参数传递和指针你就明白了。
int main()
{
char ch[100];
printf("please input char:\n");
for (int i = 0; i < 100; i++)
{
scanf_s("%c", &ch[i],100);
if (ch[i] == '\0') break;
}
for (int i = 0; i < 100; i++)
{
if ('a' <= ch[i] && ch[i] <= 'z')
{
ch[i] = ch[i] - 32;
}
else if ('A' <= ch[i] && ch[i] <= 'Z')
{
ch[i] = ch[i] + 32;
}
}
for (int i = 0; i < 100; i++)
{
printf("%c", ch[i]);
}
return 0;
}
if 为什仫执行不了呀 , 就是100个 输入20个像让输出 怎么实现呢?
\0 改为 \n 后面会出现乱码
但是勉强可以实现了
if (ch[i] == '\n') break;
@上心&: 给你一个最简单的实现,由于我用的是GCC
,对应的函数自己改:
#include <stdio.h>
#include <string.h>
int main()
{
char ch[100];
printf("please input char:\n");
scanf("%99s",ch);
for (int i = 0; i < 100; i++)
{
if ('a' <= ch[i] && ch[i] <= 'z')
{
ch[i] = ch[i] - 32;
}
else if ('A' <= ch[i] && ch[i] <= 'Z')
{
ch[i] = ch[i] + 32;
}
}
printf("%s",ch);
return 0;
}