defmaoPAO(sz): n = len(sz) #用函数len获取数组的长度存储在变量n中 for i in range (n): #用两个循环来执行冒泡排序的步骤 for j in range(n-i-1): if sz[j]>sz[j+1]: sz[j],sz[j+1]=sz[j+1],sz[j] for k in range(n): #用一个循环,按大小顺序循环输出数组中的数字 print(sz[k])
intmain() { char zifu1[]={"Hello the world"}; jc1(zifu1,0,0,0); char zifu2[]={"Python is the best LANGUAGE!"}; jc1(zifu2,0,0,0); } /*输出结果为 upper:1,lower:12 upper:9,lower:14 Program ended with exit code: 0 */
intmain()//调用函数 { char zifu1[]={"Hello the world"}; jc2(zifu1,1,'\0'); char zifu2[]={"Python is the best LANGUAGE!"}; jc2(zifu2,1,'\0'); } /*输出结果为: 最大的字符是:w 最大的字符是:y Program ended with exit code: 0*/
#include<stdio.h> intmain()//因为是填空题,所以只进行了一组测试 { int i=0; char s[120]; printf("Enter a string.\n"); scanf("%[^\n]%*c",s); //scanf函数获取含空格字符串不能使用%s,因此这里改为了%[^\n]%*c while (i<=119) { if (s[i]>='A'&&s[i]<='Z') { s[i]=s[i]-'A'+'a'; } i++; } printf("%s\n",s); } /*输出结果为: Enter a string. I LOVE pyhton! i love pyhton! */
进阶
1.设计并测试一个函数,其功能是输出输入行(长度 <=1024 )里所有的单词,并丢掉该行中其 他的字符。一个单词的定义是一串字符,其中不含空格、制表符和换行符等其它字符。例如, 输入行: 1234word?_12number +234 输出: word number
#include<stdio.h> //Name:雷博闻 Student ID:11923040207 #include"string.h"//进阶7 intmain(){ char zifu[3][80]={ //c定义一个多维字符数组并初始化 "Its name might sound a little plain, but the grey-headed albatross is the most e", "Not only a majestic bird, the grey-headed albatross is a literal a world recoreq", "In 2003, The Guinness Book of Records gave them the title of world’s fastest h" }; int i=0,j=0,upper=0,lower=0,numbers=0,others=0; while (i<=2) {j=0; //外循环,控制行数。j=0是为了重新初始化k列数 while (j<=79) { //内循环,控制列数 if((zifu[i][j]>='A')&&(zifu[i][j]<='Z')){ //判断该字符是否是大写字母,是则upper+1,不是则进行下一判断 upper++; } elseif ((zifu[i][j]>='a')&&(zifu[i][j]<='z')){ //判断该字符是否是小写字母,是则lower+1,不是则进行下一判断 lower++; } elseif ((zifu[i][j]>='0')&&(zifu[i][j]<='9')){ //判断该字符是否是数字,是则numbers+1,不是则进行下一判断 numbers++; } else{ others++; //都不满足,则others+1 } j++; } i++; } printf("大写字母的个数是:%d\n小写字母的个数是:%d\n数字的个数是在:%d\n其他字符的个数是:%d\n",upper,lower,numbers,others); //分别输出upper,lower,numbers和others的值 printf("%d\n",upper+lower+numbers+others);//输出upper,lower,numbers和others值的综合,总和为240,则程序正确 } /*输出结果: 大写字母的个数是:7 小写字母的个数是:180 数字的个数是在:4 其他字符的个数是:49 240 Program ended with exit code: 0*/