C语言成长学习题(十三)

五十六、编写求字符串长度的程序。

 1 #include <stdio.h>
 2
 3 void main(void)
 4 {
 5     char a[80];
 6     int i = 0, count = 0;
 7
 8     gets(a);
 9     while (a[i] != ‘\0‘)
10     {
11         count++;
12         i++;
13     }
14     printf("%s = %d\n", a, count);
15 }

五十七、编写字符串复制的程序。

 1 #include <stdio.h>
 2
 3 void main(void)
 4 {
 5     int i = 0;
 6     char a[50], b[50];
 7
 8     gets(a);
 9     while (a[i] != ‘\0‘)
10     {
11         b[i] = a[i];
12         i++;
13     }
14     b[i] = ‘\0‘;
15     puts(b);
16 }

五十八、编写字符串连接的程序。

 1 #include <stdio.h>
 2
 3 void main(void)
 4 {
 5     int i = 0, j = 0;
 6     char a[80], b[30];
 7
 8     gets(a);
 9     gets(b);
10     while (a[i] != ‘\0‘)
11         i++;
12     while (b[j] != ‘\0‘)
13         a[i++] = b[j++];
14     a[i] = ‘\0‘;
15     puts(a);
16 }

五十九、编写字符串比较的程序。

 1 #include <stdio.h>
 2
 3 void main(void)
 4 {
 5     int i = 0;
 6     char a[30], b[30];
 7
 8     gets(a);
 9     gets(b);
10
11     while (a[i] = b[i] && a[i] !=‘\0‘)
12         i++;
13     if (a[i] > b[i])
14         printf("第一个字符串大于第二个字符串.\n");
15     else if (a[i] == b[i])
16         printf("两个字符串相等.\n");
17     else
18         printf("第一个字符串小于第二个字符串.\n");
19 }

六十、打印杨辉三角形。

 1 #include <stdio.h>
 2 #include <math.h>
 3
 4 #define N 6
 5
 6 void main(void)
 7 {
 8     int a[N][N], i, j, k, spaces;
 9     for (i = 0; i < N; i++)
10         a[i][0] = a[i][i] = 1;
11     for (i = 2; i < N; i++)
12         for (j = 1; j < i; j++)
13             a[i][j] = a[i-1][j-1] + a[i-1][j];
14     for (i = 0; i < N; i++)
15     {
16         spaces = (N-i-1) * 3;
17         for (k = 0; k < spaces; k++)
18             printf(" ");
19         for (j = 0; j <= i; j++)
20             printf("%6d", a[i][j]);
21         printf("\n");
22     }
23 }

结果:

1

1     2     1

1     3     3      1

1     4     6     4     1

1     5    10    10     5     1

时间: 2024-08-05 16:07:39

C语言成长学习题(十三)的相关文章

C语言成长学习题(十四)

六十一.定义4*6的实型数组,并将各行前5列元素的平均值分别放在同一行的第6列上. 1 #include <stdio.h> 2 3 void main(void) 4 { 5 float a[4][6], sum; 6 int i, j; 7 8 for (i = 0; i < 4; i++) 9 for (j = 0; j < 5; j++) 10 a[i][j] = i * j + 1; 11 for (i = 0; i < 4; i++) 12 { 13 sum =

C语言成长学习题(四)

十三.编写输出如下分段函数值的程序,要求x的值从键盘输入. 1 #include <stdio.h> 2 3 void main(void) 4 { 5 float x, y; 6 7 printf("Input x:\n"); 8 scanf("%f", &x); 9 if(x <= 0) 10 y = x + 1; 11 else if(x <= 1) 12 y = 1; 13 else 14 y = x; 15 printf(&

C语言成长学习题(十五)

66.编写字符串复制的程序(用指针变量处理). 1 #include <stdio.h> 2 3 void main(void) 4 { 5 char a[50], b[80], *p, *q; 6 7 p = a; 8 q = b; 9 printf("Input data: "); 10 gets(a); 11 while (*p != '\0') 12 *q++ = *p++; 13 *q = '\0'; 14 puts(b); 15 } Mark: 复制操作结束后,

C语言成长学习题(十)

四十一.编写实现以下功能的程序:若从键盘输入字符Y(或y)或N(或n),则终止循环,否则一直等待输入. 1 #include <stdio.h> 2 #include <conio.h> 3 4 void main(void) 5 { 6 char c; 7 8 printf("是否继续(Y/N)?\n"); 9 do 10 { 11 c = getch(); 12 if (c == 'Y' || c == 'y' || c == 'N' || c =='n')

C语言成长学习题(十六)

72.假设一维数组中存放互不相同的十个整数,要求根据输入的下标值,即可直接删除. 1 #include <stdio.h> 2 3 int mydel (int *a, int n, int k) 4 { 5 int i; 6 7 for (i = k; i < n - 1; i++) 8 *(a+i) = *(a+i+1); 9 n--; 10 11 return n; 12 } 13 14 void myout (int *a, int n) 15 { 16 while (n >

C语言成长学习题(三)

十.编写一个含有逻辑表达式的程序. 1 #include <stdio.h> 2 3 void main(void) 4 { 5 int x; 6 7 x = 1; 8 printf("%d ", x>=0 && x<=2); 9 10 x = 5; 11 printf("%d ", x>=0 && x<=2); 12 printf("%d ", x<-3 || x>

C语言成长学习题(七)

二十六.输出1!.2!.3!.4!.....n!的值. 1 #include <stdio.h> 2 3 void main(void) 4 { 5 int i, n; 6 float fac; 7 8 printf("Input n: "); 9 scanf("%d", &n); 10 for (i = 1; i<=n; i++) 11 { 12 fac = fac * i; 13 printf("%d! = %.0f\n&qu

C语言成长学习题(五)

十七.求一元二次方程ax2+bx+c=0的实根(要求a.b.c的值从键盘输入,a!=0). 1 #include <stdio.h> 2 #include <math.h> 3 4 void main(void) 5 { 6 int a, b, c; 7 float delta, x1, x2; 8 9 printf("Input a, b, c:\n"); 10 scanf("%d%d%d", &a, &b, &c)

【C语言天天练(十三)】printf、fprintf、sprintf和snprintf函数

#include <stdio.h> int printf(const char *format, ...); int fprintf(FILE *stream, const char *format, ...); int sprintf(char *str, const char *format, ...); int snprintf(char *str, size_t size, const char *format, ...); printf是标准的输出函数. fprintf传送格式化输