1. 编写一程序,实现从键盘上输入的一字符串,将其中的小写字母变成大写,大写变成小写。
2.从键盘输入整数,统计正数和负数的个数,当输入为0时统计结束。
3.求1到100之间的所有素数。
----------------解决方案--------------------------------------------------------
自己做吧,动一下脑筋,不然就不要学了!
----------------解决方案--------------------------------------------------------
1.
#include <stdio.h>
main(_){while((_=getch())!=10)putch(_>='a'&&_<='z'?_+'A'-'a':_>='A'&&_<='Z'?_-'A'+'a':_);}
2.
#include <stdio.h>
int a[2];
main(_)
{ for(;_;scanf("%d",&_),a[_>0]++,_==0&&printf(">0 :%d <0 :%d\n",a[1],--a[0]));
}
or
#include <stdio.h>
main(_,a,b)
{ for(a=b=0;_;scanf("%d",&_),a+=_>0,b+=_<0,_==0&&printf(">0 :%d <0 :%d",a,b));
}
----------------解决方案--------------------------------------------------------
上面的knocker 老兄,你的程序真是太经典了 不过人家初学,不要写的这么简短么,写长一点,让人家多学点么,呵呵 看我来写个长的; #include <stdio.h> #define DEBUG #define CHAR_NUM 50 typedef unsigned SIZE_T;
void *MEMSET(void *s, int c, SIZE_T n); void CONVERT(char *s);
void main(void) { FILE *fp; char ch[CHAR_NUM];
if((fp = fopen("out.txt", "w")) == 0) { #ifdef DEBUG printf("Can not open file!\n"); #endif return; } MEMSET(ch, 0, sizeof(ch)); printf("Please Input String:"); gets(ch); CONVERT(ch); #ifdef DEBUG printf("after convert the string is:%s\n", ch); #endif fprintf(fp, "%s", ch); fclose(fp); }
void *MEMSET(void *s, int c, SIZE_T n) { unsigned char *st = (unsigned char *)s; unsigned char ch = c;
while (n-- > 0) *st++ = ch; return s; }
void CONVERT(char *s) { while(*s) { if(*s >= 'A' && *s <= 'Z') { *s += 32; } else if(*s >= 'a' && *s <= 'z') { *s -= 32; } else ;
s++; } }
----------------解决方案--------------------------------------------------------
//我写了第一个小题的,不知道是否可以,请指教! //注:用VC通过编译的。 #include <stdio.h> #include <stdlib.h>
void main() { char *ch; int i; ch=(char*) malloc(sizeof(char)); printf("Please input:\n"); gets(ch); for(i=0;*ch!='\0';ch++,i++) { if(*ch>='a'&&*ch<='z'){ *ch=(char)(*ch-('a'-'A')); continue; } if(*ch>='A'&&*ch<='Z'){ *ch=(char)(*ch+('a'-'A')); } } ch=ch-i; printf("The converted string : \n"); printf("%s",ch); }
----------------解决方案--------------------------------------------------------
呵呵,都挺不错的!!
----------------解决方案--------------------------------------------------------
上面的knocker 老兄,你的程序真是太经典了 不过人家初学,不要写的这么简短么,写长一点,让人家多学点么,呵呵 看我来写个长的; #include <stdio.h> #define DEBUG #define CHAR_NUM 50 typedef unsigned SIZE_T;
void *MEMSET(void *s, int c, SIZE_T n); void CONVERT(char *s);
void main(void) { FILE *fp; char ch[CHAR_NUM];
if((fp = fopen("out.txt", "w")) == 0) { #ifdef DEBUG printf("Can not open file!\n"); #endif return; } MEMSET(ch, 0, sizeof(ch)); printf("Please Input String:"); gets(ch); CONVERT(ch); #ifdef DEBUG printf("after convert the string is:%s\n", ch); #endif fprintf(fp, "%s", ch); fclose(fp); }
void *MEMSET(void *s, int c, SIZE_T n) { unsigned char *st = (unsigned char *)s; unsigned char ch = c;
while (n-- > 0) *st++ = ch; return s; }
void CONVERT(char *s) { while(*s) { if(*s >= 'A' && *s <= 'Z') { *s += 32; } else if(*s >= 'a' && *s <= 'z') { *s -= 32; } else ;
s++; } }
确实不错,不过有点故意凋难的感觉……
----------------解决方案--------------------------------------------------------