从键盘输入若干行文本,每行以回车结束,以 ctrl+z 作为输入结束符,统计其行数。
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
int main()
{
int c,num=0;
while(1)
{
c=getche();
if (c==13)
{
printf("\n");
num++;
}
if(c==26)
{
printf("\n您输入了 %d 行\n",++num);
exit(1);
}
}
}
从键盘输入字符,以 ctrl+z 结束,统计输入的数字 0~9、空白符和其它字符的个数。
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
int main()
{
int counter_num=0,counter_space=0,counter_other=-1;
char c;
while(1)
{
c=getche();
if (c>='0'&&c<='9')
counter_num++;
else if (c==' ')
counter_space++;
else
counter_other++;
if(c==26)
{
printf("\n\ncounter_num=%d\tcounter_space=%d\t,counter_other=%d\n",counter_num,counter_space,counter_other);
exit(1);
}
}
}
键盘输入一个字符串(长度不超过20,其中不含空格),将其复制一份,复制时将小写字母都转换成为大写字母。
#include <stdio.h>
void main()
{
char a[20];
int i;
printf("请输入一个由英文字母构成的字符串:\n");
gets(a);
for (i=0;i<20;i++)
if(a[i]>='a'&&a[i]<='z')
a[i]-=32;
a[20]='\0';
puts(a);
}