• 第十一次作业


    一、实验内容      

          1.输入一个字符串,统计大写字母、小写字母、空格、数字和其他字符的个数。(要求用字符数组)

    #include<stdio.h>
    #define N 100
    int main()
    {
    	char shuzu[N];
    	int a,b,c,d,i,e;
    	a=0;
    	b=0;
    	c=0;
    	d=0;
    	e=0;
    	printf("请输入字符串
    ");
    	gets(shuzu);
    	for(i=0;shuzu[i]!='';i++)
    	   {
    		 if ('A'<=shuzu[i]&&shuzu[i]<='Z')
    	       a++;
    	    else if('a'<=shuzu[i]&&shuzu[i]<='z')
    	       e++;
    	    else if(shuzu[i]==' ')
    	       b++;
    	    else if('0'<=shuzu[i]&&shuzu[i]<='9')
    	       c++;
    	    else 
    	       d++;
    	   }
    	    printf("大写字母:%d
    ",a);
    	    printf("小写字母:%d
    ",e);
    	    printf("空格:%d
    ",b);
    	    printf("数字:%d
    ",c);
    	    printf("其他字符:%d
    ",d);
    	    return 0;
    } 
    

      

      2.利用字符数组进行密码的验证,如果密码正确则登陆成功,否则登录失败。密码允许输入三次。

    #include<stdio.h>
    #include<string.h>
    #define N 100
    int main()
    {
    	char password[N],password0[N];
    	int i;
    	printf("请设置一个密码,:
    ");
    	gets(password);
    	printf("请输入密码:
    ");
    	gets(password0);
    	if (strcmp(password,password0)==0)
    	{
    		printf("登陆成功
    ");
    	}
    	else 
    	{
    			for(i=0;i<3;i++)
    		{
    		     printf("登录失败,请重新输入
    ");
    		     gets(password0);
    		     if (strcmp(password,password0)==0)
    			{
    				printf("登陆成功
    ");
      break; } } } }

      

      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]!='';i++)
    	{
    		if(palindrome[i]==palindrome[b-i-1])
    		{
    			return 1;
    		}
    		else
    		return 0;
    	}
    }
    

      

      

    二、实验总结

    1、字符串不能用关系运算符>、<、==直接比较大小,如 strcmp(password,password0)==0是判断两个password,password0是否相等。

    2、scanf函数和gets函数的区别 (1)对空格的处理机制不同 scanf函数不能输入带空格的字符串 gets函数可以输入带空格的字符串 (2)对回车换行符的处理机制不同

    3、int c;  while((c = getchar()) != ' ' && c != EOF); 清除缓存。

    三、课程体会(请回答以下问题)

        1. 经过一个学期的学习,你统计过一共写了多少行代码吗?与开学初相比,你对C语言或者程序设计有了哪些认识和理解?

             大约1500行左右  ,C语言需要我们多练习,多总结

        2. 在C语言的学习过程中,你有什么经验和教训分享给大家?

         多加练习,自己学的不怎么好,还是需要多练习,搞清逻辑关系。

        3. 对于采用技术博客提交作业这种形式,你有什么看法,对你有帮助的地方在哪里?你觉得还可以做哪些改进?,如果下一届的学弟学妹们入学了,你会给他们推荐这种教学形式吗?。

       采用博客园,能使自己每周都有个任务在心里,能起到督促的作用,多练一遍,也能看到其他同学的作业,相互之间互相交流一下,更加促进学习。会像他们推荐这种教学形式,可以锻炼自己更多的能力

  • 相关阅读:
    单片机学习01__跑起你的流水灯
    python2与python3共存
    rpi-kali 搭建网络靶场
    P3388 【模板】割点(割顶)
    P3387 【模板】缩点
    P1069 细胞分裂
    The Unique MST[不严格的次小生成树]
    P3369 【模板】普通平衡树
    Netty的线程模型可不是Reactor这么简单
    SpringBoot+Mybatis+MySQL实现读写分离
  • 原文地址:https://www.cnblogs.com/lch1/p/6185341.html
Copyright © 2020-2023  润新知