1.输入一个字符串,统计大写字母、小写字母、空格、数字和其他字符的个数。(要求用字符数组)
#include<stdio.h> int main() { char s[100]; int d,x,y,z,q,i; gets(s); d=x=y=z=q=0; for(i=0;s[i];i++) if(s[i]>='A' && s[i]<='Z')d++; else if(s[i]>='a' && s[i]<='z')x++; else if(s[i]>='0' && s[i]<='9')y++; else if(s[i]==' ')z++; else q++; printf("大写字母有%d个 小写字母有 %d个 数字有 %d个 空格有%d个 其他字符有 %d个 ",d,x,y,z,q); }
2.利用字符数组进行密码的验证,如果密码正确则登陆成功,否则登录失败。密码允许输入三次。
#include <stdio.h> #include <string.h> int main() { char password[]="secret",intputa[8],intputb[8],intputc[8]; printf("please input the password "); scanf("%7s",intputa); if(strcmp(password,intputa)==0) { printf("welcome! "); } else { printf("wrong password! "); printf("please input the password "); scanf("%7s",intputb); if(strcmp(password,intputb)==0) { printf("welcome! "); } else { printf("wrong password! "); printf("please input the password "); scanf("%7s",intputc); if(strcmp(password,intputc)==0) { printf("welcome! "); } else { printf("你已经没有机会了 "); } } } return 0; }
3.编写一个函数,判断一个字符串是否是回文。若是回文函数返回值为1;否则返回值为0。回文是顺读和倒读都一样。如“level”“abba”等是回文,但“abcd”不是回文。在主函数中调用回文函数对输入的字符串进行判断。
#include<stdio.h> #include<string.h> #define N 100 int huiwen(char palindrome[N]); int main() { char palindrome[N]; int i; printf("输入一个字符串 "); gets(palindrome); i=huiwen(palindrome); if (i==1) { printf("是回文 "); } else if(i==0) { printf("不是回文 "); } } int huiwen(char palindrome[N]) { int i,b; b=strlen(palindrome); for(i=0;palindrome[i]!='