返回:贺老师课程教学链接
写出下面程序的运行结果。
(1)
#include <stdio.h> int main() { int i,c,j=0; char s1[2][5]= {"8980","9198"}, s2[5]= {'\0'}; for (i=3; i>=0 ; i--) { c=s1[0][i]+s1[1][i]-2*'0'; s2[i] = (c+j)%10 + '0' ; j = (c+j)/10; } puts(s2); return 0; }
(2)
#include <stdio.h> int main() { char *a[]= {"Pascal","C Language","dBase","Java"}; char **p; int j ; p = a + 3; for (j=3; j>=0; j--) printf("%s\n",*(p--)) ; return 0; }
(3)
#include<stdio.h> #include <string.h> int main() { char str[10][80],c[80]; int i; for(i=0; i<10; i++) gets(str[i]); strcpy(c,str[0]); for(i=1; i<10; i++) if(strlen(c)<strlen(str[i])) strcpy(c,str[i]); puts(c); return 0; }
(4)
#include<stdio.h> #include <string.h> #define N 10 int main() { char str[20],s[N][20]; int i; for (i=0; i<N; i++) gets(s[i]); strcpy(str,s[0]); for(i=1; i<N; i++) if (strcmp(str, s[i])>0) strcpy(str,s[i]); printf("The longest string is : \n%s\n",str); return 0; }
(5)
#include <stdio.h> int main() { char *days[] = { "Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday" }; int i; printf("Work days: "); for (i = 1; i < 6; ++i) printf("%s ", days[i]); printf("\nWeekend: "); printf("%s %s\n", days[6], days[0]); return 0; }
(6)
#include<stdio.h> #include<string.h> void f(char p[][10],int n); int main() { char p[][10]= {"China","America","Russia","England","France"}; int i; f(p,5); for(i=0; i<5; ++i) printf("%s ", p[i]); printf("\n"); return 0; } void f(char p[][10],int n) { char t[10]; int i,j; for(i=0; i<n-1; i++) for(j=i+1; j<n; j++) if(strcmp(p[i],p[j])<0) { strcpy(t,p[i]); strcpy(p[i],p[j]); strcpy(p[j],t); } }
时间: 2024-09-25 15:24:57