您的位置:首页 > 编程语言 > C语言/C++

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

2015-12-07 20:43 246 查看
五十六、编写求字符串长度的程序。

#include <stdio.h>

void main(void)
{
char a[80];
int i = 0, count = 0;

gets(a);
while (a[i] != '\0')
{
count++;
i++;
}
printf("%s = %d\n", a, count);
}


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

#include <stdio.h>

void main(void)
{
int i = 0;
char a[50], b[50];

gets(a);
while (a[i] != '\0')
{
b[i] = a[i];
i++;
}
b[i] = '\0';
puts(b);
}


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

#include <stdio.h>

void main(void)
{
int i = 0, j = 0;
char a[80], b[30];

gets(a);
gets(b);
while (a[i] != '\0')
i++;
while (b[j] != '\0')
a[i++] = b[j++];
a[i] = '\0';
puts(a);
}


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

#include <stdio.h>

void main(void)
{
int i = 0;
char a[30], b[30];

gets(a);
gets(b);

while (a[i] = b[i] && a[i] !='\0')
i++;
if (a[i] > b[i])
printf("第一个字符串大于第二个字符串.\n");
else if (a[i] == b[i])
printf("两个字符串相等.\n");
else
printf("第一个字符串小于第二个字符串.\n");
}


六十、打印杨辉三角形。

#include <stdio.h>
#include <math.h>

#define N 6

void main(void)
{
int a

, i, j, k, spaces;
for (i = 0; i < N; i++)
a[i][0] = a[i][i] = 1;
for (i = 2; i < N; i++)
for (j = 1; j < i; j++)
a[i][j] = a[i-1][j-1] + a[i-1][j];
for (i = 0; i < N; i++)
{
spaces = (N-i-1) * 3;
for (k = 0; k < spaces; k++)
printf(" ");
for (j = 0; j <= i; j++)
printf("%6d", a[i][j]);
printf("\n");
}
}


结果:

1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: