从键盘输入一个字符串,将其中的大写字母变小写字母,小写字母变大写字母,并输出 。

/*2、编程题
  1) 从键盘输入一个字符串,将其中的大写字母变小写字母,小写字母变大写字母,并输出 。
【要求】
  (1)用字符数组表示存储字符串(字符串最大为100)。
  (2)使用scanf函数逐个输入字符保存在字符数组中
  (3)使用printf函数逐个输出字符数组中的字符
*/

题目很简单,但是要求用scanf函数逐个输入字符保存在字符数组中,对于新手有一些难度。

 

#include<stdio.h>
void main()
{
int i=0;
char a[100],c;

printf(
"请输入字符串的内容:\t");
do{
scanf(
"%c",&a[i]);
c
=a[i];
i
++;
}
while(c!='\n');

a[i]
='\0';

i
=0;
printf(
"输入字符串的内容为:\t");
while(a[i]!='\0')
{
printf(
"%c",a[i]);
i
++;
}
printf(
"转换后字符串的内容为:\t");
i
=0;
while(a[i]!='\0')
{
c
=a[i];
if(c>='a' && c<='z')
a[i]
-=32;
else if(c>='A' && c<='Z')
a[i]
+=32;
printf(
"%c",a[i]);
i
++;
}
}

 

posted @ 2010-02-11 10:22  齐心  Views(42109)  Comments(0Edit  收藏  举报